ac25d2bfa90b92fb2739d620e06031a990eab4b2
[linux-2.6-microblaze.git] / drivers / scsi / qla2xxx / qla_init.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * QLogic Fibre Channel HBA Driver
4  * Copyright (c)  2003-2014 QLogic Corporation
5  */
6 #include "qla_def.h"
7 #include "qla_gbl.h"
8
9 #include <linux/delay.h>
10 #include <linux/slab.h>
11 #include <linux/vmalloc.h>
12
13 #include "qla_devtbl.h"
14
15 #ifdef CONFIG_SPARC
16 #include <asm/prom.h>
17 #endif
18
19 #include "qla_target.h"
20
21 /*
22 *  QLogic ISP2x00 Hardware Support Function Prototypes.
23 */
24 static int qla2x00_isp_firmware(scsi_qla_host_t *);
25 static int qla2x00_setup_chip(scsi_qla_host_t *);
26 static int qla2x00_fw_ready(scsi_qla_host_t *);
27 static int qla2x00_configure_hba(scsi_qla_host_t *);
28 static int qla2x00_configure_loop(scsi_qla_host_t *);
29 static int qla2x00_configure_local_loop(scsi_qla_host_t *);
30 static int qla2x00_configure_fabric(scsi_qla_host_t *);
31 static int qla2x00_find_all_fabric_devs(scsi_qla_host_t *);
32 static int qla2x00_restart_isp(scsi_qla_host_t *);
33
34 static struct qla_chip_state_84xx *qla84xx_get_chip(struct scsi_qla_host *);
35 static int qla84xx_init_chip(scsi_qla_host_t *);
36 static int qla25xx_init_queues(struct qla_hw_data *);
37 static void qla24xx_handle_gpdb_event(scsi_qla_host_t *vha,
38                                       struct event_arg *ea);
39 static void qla24xx_handle_prli_done_event(struct scsi_qla_host *,
40     struct event_arg *);
41 static void __qla24xx_handle_gpdb_event(scsi_qla_host_t *, struct event_arg *);
42
43 /* SRB Extensions ---------------------------------------------------------- */
44
45 void
46 qla2x00_sp_timeout(struct timer_list *t)
47 {
48         srb_t *sp = from_timer(sp, t, u.iocb_cmd.timer);
49         struct srb_iocb *iocb;
50
51         WARN_ON(irqs_disabled());
52         iocb = &sp->u.iocb_cmd;
53         iocb->timeout(sp);
54
55         /* ref: TMR */
56         kref_put(&sp->cmd_kref, qla2x00_sp_release);
57 }
58
59 void qla2x00_sp_free(srb_t *sp)
60 {
61         struct srb_iocb *iocb = &sp->u.iocb_cmd;
62
63         del_timer(&iocb->timer);
64         qla2x00_rel_sp(sp);
65 }
66
67 void qla2xxx_rel_done_warning(srb_t *sp, int res)
68 {
69         WARN_ONCE(1, "Calling done() of an already freed srb %p object\n", sp);
70 }
71
72 void qla2xxx_rel_free_warning(srb_t *sp)
73 {
74         WARN_ONCE(1, "Calling free() of an already freed srb %p object\n", sp);
75 }
76
77 /* Asynchronous Login/Logout Routines -------------------------------------- */
78
79 unsigned long
80 qla2x00_get_async_timeout(struct scsi_qla_host *vha)
81 {
82         unsigned long tmo;
83         struct qla_hw_data *ha = vha->hw;
84
85         /* Firmware should use switch negotiated r_a_tov for timeout. */
86         tmo = ha->r_a_tov / 10 * 2;
87         if (IS_QLAFX00(ha)) {
88                 tmo = FX00_DEF_RATOV * 2;
89         } else if (!IS_FWI2_CAPABLE(ha)) {
90                 /*
91                  * Except for earlier ISPs where the timeout is seeded from the
92                  * initialization control block.
93                  */
94                 tmo = ha->login_timeout;
95         }
96         return tmo;
97 }
98
99 static void qla24xx_abort_iocb_timeout(void *data)
100 {
101         srb_t *sp = data;
102         struct srb_iocb *abt = &sp->u.iocb_cmd;
103         struct qla_qpair *qpair = sp->qpair;
104         u32 handle;
105         unsigned long flags;
106
107         if (sp->cmd_sp)
108                 ql_dbg(ql_dbg_async, sp->vha, 0x507c,
109                     "Abort timeout - cmd hdl=%x, cmd type=%x hdl=%x, type=%x\n",
110                     sp->cmd_sp->handle, sp->cmd_sp->type,
111                     sp->handle, sp->type);
112         else
113                 ql_dbg(ql_dbg_async, sp->vha, 0x507c,
114                     "Abort timeout 2 - hdl=%x, type=%x\n",
115                     sp->handle, sp->type);
116
117         spin_lock_irqsave(qpair->qp_lock_ptr, flags);
118         for (handle = 1; handle < qpair->req->num_outstanding_cmds; handle++) {
119                 if (sp->cmd_sp && (qpair->req->outstanding_cmds[handle] ==
120                     sp->cmd_sp))
121                         qpair->req->outstanding_cmds[handle] = NULL;
122
123                 /* removing the abort */
124                 if (qpair->req->outstanding_cmds[handle] == sp) {
125                         qpair->req->outstanding_cmds[handle] = NULL;
126                         break;
127                 }
128         }
129         spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
130
131         if (sp->cmd_sp) {
132                 /*
133                  * This done function should take care of
134                  * original command ref: INIT
135                  */
136                 sp->cmd_sp->done(sp->cmd_sp, QLA_OS_TIMER_EXPIRED);
137         }
138
139         abt->u.abt.comp_status = cpu_to_le16(CS_TIMEOUT);
140         sp->done(sp, QLA_OS_TIMER_EXPIRED);
141 }
142
143 static void qla24xx_abort_sp_done(srb_t *sp, int res)
144 {
145         struct srb_iocb *abt = &sp->u.iocb_cmd;
146         srb_t *orig_sp = sp->cmd_sp;
147
148         if (orig_sp)
149                 qla_wait_nvme_release_cmd_kref(orig_sp);
150
151         if (sp->flags & SRB_WAKEUP_ON_COMP)
152                 complete(&abt->u.abt.comp);
153         else
154                 /* ref: INIT */
155                 kref_put(&sp->cmd_kref, qla2x00_sp_release);
156 }
157
158 int qla24xx_async_abort_cmd(srb_t *cmd_sp, bool wait)
159 {
160         scsi_qla_host_t *vha = cmd_sp->vha;
161         struct srb_iocb *abt_iocb;
162         srb_t *sp;
163         int rval = QLA_FUNCTION_FAILED;
164
165         /* ref: INIT for ABTS command */
166         sp = qla2xxx_get_qpair_sp(cmd_sp->vha, cmd_sp->qpair, cmd_sp->fcport,
167                                   GFP_ATOMIC);
168         if (!sp)
169                 return QLA_MEMORY_ALLOC_FAILED;
170
171         abt_iocb = &sp->u.iocb_cmd;
172         sp->type = SRB_ABT_CMD;
173         sp->name = "abort";
174         sp->qpair = cmd_sp->qpair;
175         sp->cmd_sp = cmd_sp;
176         if (wait)
177                 sp->flags = SRB_WAKEUP_ON_COMP;
178
179         init_completion(&abt_iocb->u.abt.comp);
180         /* FW can send 2 x ABTS's timeout/20s */
181         qla2x00_init_async_sp(sp, 42, qla24xx_abort_sp_done);
182         sp->u.iocb_cmd.timeout = qla24xx_abort_iocb_timeout;
183
184         abt_iocb->u.abt.cmd_hndl = cmd_sp->handle;
185         abt_iocb->u.abt.req_que_no = cpu_to_le16(cmd_sp->qpair->req->id);
186
187         ql_dbg(ql_dbg_async, vha, 0x507c,
188                "Abort command issued - hdl=%x, type=%x\n", cmd_sp->handle,
189                cmd_sp->type);
190
191         rval = qla2x00_start_sp(sp);
192         if (rval != QLA_SUCCESS) {
193                 /* ref: INIT */
194                 kref_put(&sp->cmd_kref, qla2x00_sp_release);
195                 return rval;
196         }
197
198         if (wait) {
199                 wait_for_completion(&abt_iocb->u.abt.comp);
200                 rval = abt_iocb->u.abt.comp_status == CS_COMPLETE ?
201                         QLA_SUCCESS : QLA_ERR_FROM_FW;
202                 /* ref: INIT */
203                 kref_put(&sp->cmd_kref, qla2x00_sp_release);
204         }
205
206         return rval;
207 }
208
209 void
210 qla2x00_async_iocb_timeout(void *data)
211 {
212         srb_t *sp = data;
213         fc_port_t *fcport = sp->fcport;
214         struct srb_iocb *lio = &sp->u.iocb_cmd;
215         int rc, h;
216         unsigned long flags;
217
218         if (fcport) {
219                 ql_dbg(ql_dbg_disc, fcport->vha, 0x2071,
220                     "Async-%s timeout - hdl=%x portid=%06x %8phC.\n",
221                     sp->name, sp->handle, fcport->d_id.b24, fcport->port_name);
222
223                 fcport->flags &= ~(FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE);
224         } else {
225                 pr_info("Async-%s timeout - hdl=%x.\n",
226                     sp->name, sp->handle);
227         }
228
229         switch (sp->type) {
230         case SRB_LOGIN_CMD:
231                 rc = qla24xx_async_abort_cmd(sp, false);
232                 if (rc) {
233                         /* Retry as needed. */
234                         lio->u.logio.data[0] = MBS_COMMAND_ERROR;
235                         lio->u.logio.data[1] =
236                                 lio->u.logio.flags & SRB_LOGIN_RETRIED ?
237                                 QLA_LOGIO_LOGIN_RETRIED : 0;
238                         spin_lock_irqsave(sp->qpair->qp_lock_ptr, flags);
239                         for (h = 1; h < sp->qpair->req->num_outstanding_cmds;
240                             h++) {
241                                 if (sp->qpair->req->outstanding_cmds[h] ==
242                                     sp) {
243                                         sp->qpair->req->outstanding_cmds[h] =
244                                             NULL;
245                                         break;
246                                 }
247                         }
248                         spin_unlock_irqrestore(sp->qpair->qp_lock_ptr, flags);
249                         sp->done(sp, QLA_FUNCTION_TIMEOUT);
250                 }
251                 break;
252         case SRB_LOGOUT_CMD:
253         case SRB_CT_PTHRU_CMD:
254         case SRB_MB_IOCB:
255         case SRB_NACK_PLOGI:
256         case SRB_NACK_PRLI:
257         case SRB_NACK_LOGO:
258         case SRB_CTRL_VP:
259         default:
260                 rc = qla24xx_async_abort_cmd(sp, false);
261                 if (rc) {
262                         spin_lock_irqsave(sp->qpair->qp_lock_ptr, flags);
263                         for (h = 1; h < sp->qpair->req->num_outstanding_cmds;
264                             h++) {
265                                 if (sp->qpair->req->outstanding_cmds[h] ==
266                                     sp) {
267                                         sp->qpair->req->outstanding_cmds[h] =
268                                             NULL;
269                                         break;
270                                 }
271                         }
272                         spin_unlock_irqrestore(sp->qpair->qp_lock_ptr, flags);
273                         sp->done(sp, QLA_FUNCTION_TIMEOUT);
274                 }
275                 break;
276         }
277 }
278
279 static void qla2x00_async_login_sp_done(srb_t *sp, int res)
280 {
281         struct scsi_qla_host *vha = sp->vha;
282         struct srb_iocb *lio = &sp->u.iocb_cmd;
283         struct event_arg ea;
284
285         ql_dbg(ql_dbg_disc, vha, 0x20dd,
286             "%s %8phC res %d \n", __func__, sp->fcport->port_name, res);
287
288         sp->fcport->flags &= ~(FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE);
289
290         if (!test_bit(UNLOADING, &vha->dpc_flags)) {
291                 memset(&ea, 0, sizeof(ea));
292                 ea.fcport = sp->fcport;
293                 ea.data[0] = lio->u.logio.data[0];
294                 ea.data[1] = lio->u.logio.data[1];
295                 ea.iop[0] = lio->u.logio.iop[0];
296                 ea.iop[1] = lio->u.logio.iop[1];
297                 ea.sp = sp;
298                 if (res)
299                         ea.data[0] = MBS_COMMAND_ERROR;
300                 qla24xx_handle_plogi_done_event(vha, &ea);
301         }
302
303         /* ref: INIT */
304         kref_put(&sp->cmd_kref, qla2x00_sp_release);
305 }
306
307 int
308 qla2x00_async_login(struct scsi_qla_host *vha, fc_port_t *fcport,
309     uint16_t *data)
310 {
311         srb_t *sp;
312         struct srb_iocb *lio;
313         int rval = QLA_FUNCTION_FAILED;
314
315         if (!vha->flags.online || (fcport->flags & FCF_ASYNC_SENT) ||
316             fcport->loop_id == FC_NO_LOOP_ID) {
317                 ql_log(ql_log_warn, vha, 0xffff,
318                     "%s: %8phC - not sending command.\n",
319                     __func__, fcport->port_name);
320                 return rval;
321         }
322
323         /* ref: INIT */
324         sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
325         if (!sp)
326                 goto done;
327
328         qla2x00_set_fcport_disc_state(fcport, DSC_LOGIN_PEND);
329         fcport->flags |= FCF_ASYNC_SENT;
330         fcport->logout_completed = 0;
331
332         sp->type = SRB_LOGIN_CMD;
333         sp->name = "login";
334         sp->gen1 = fcport->rscn_gen;
335         sp->gen2 = fcport->login_gen;
336         qla2x00_init_async_sp(sp, qla2x00_get_async_timeout(vha) + 2,
337                               qla2x00_async_login_sp_done);
338
339         lio = &sp->u.iocb_cmd;
340         if (N2N_TOPO(fcport->vha->hw) && fcport_is_bigger(fcport)) {
341                 lio->u.logio.flags |= SRB_LOGIN_PRLI_ONLY;
342         } else {
343                 if (vha->hw->flags.edif_enabled &&
344                     DBELL_ACTIVE(vha)) {
345                         lio->u.logio.flags |=
346                                 (SRB_LOGIN_FCSP | SRB_LOGIN_SKIP_PRLI);
347                 } else {
348                         lio->u.logio.flags |= SRB_LOGIN_COND_PLOGI;
349                 }
350         }
351
352         if (NVME_TARGET(vha->hw, fcport))
353                 lio->u.logio.flags |= SRB_LOGIN_SKIP_PRLI;
354
355         rval = qla2x00_start_sp(sp);
356
357         ql_dbg(ql_dbg_disc, vha, 0x2072,
358                "Async-login - %8phC hdl=%x, loopid=%x portid=%06x retries=%d %s.\n",
359                fcport->port_name, sp->handle, fcport->loop_id,
360                fcport->d_id.b24, fcport->login_retry,
361                lio->u.logio.flags & SRB_LOGIN_FCSP ? "FCSP" : "");
362
363         if (rval != QLA_SUCCESS) {
364                 fcport->flags |= FCF_LOGIN_NEEDED;
365                 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
366                 goto done_free_sp;
367         }
368
369         return rval;
370
371 done_free_sp:
372         /* ref: INIT */
373         kref_put(&sp->cmd_kref, qla2x00_sp_release);
374         fcport->flags &= ~FCF_ASYNC_SENT;
375 done:
376         fcport->flags &= ~FCF_ASYNC_ACTIVE;
377         return rval;
378 }
379
380 static void qla2x00_async_logout_sp_done(srb_t *sp, int res)
381 {
382         sp->fcport->flags &= ~(FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE);
383         sp->fcport->login_gen++;
384         qlt_logo_completion_handler(sp->fcport, sp->u.iocb_cmd.u.logio.data[0]);
385         /* ref: INIT */
386         kref_put(&sp->cmd_kref, qla2x00_sp_release);
387 }
388
389 int
390 qla2x00_async_logout(struct scsi_qla_host *vha, fc_port_t *fcport)
391 {
392         srb_t *sp;
393         int rval = QLA_FUNCTION_FAILED;
394
395         fcport->flags |= FCF_ASYNC_SENT;
396         /* ref: INIT */
397         sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
398         if (!sp)
399                 goto done;
400
401         sp->type = SRB_LOGOUT_CMD;
402         sp->name = "logout";
403         qla2x00_init_async_sp(sp, qla2x00_get_async_timeout(vha) + 2,
404                               qla2x00_async_logout_sp_done),
405
406         ql_dbg(ql_dbg_disc, vha, 0x2070,
407             "Async-logout - hdl=%x loop-id=%x portid=%02x%02x%02x %8phC explicit %d.\n",
408             sp->handle, fcport->loop_id, fcport->d_id.b.domain,
409                 fcport->d_id.b.area, fcport->d_id.b.al_pa,
410                 fcport->port_name, fcport->explicit_logout);
411
412         rval = qla2x00_start_sp(sp);
413         if (rval != QLA_SUCCESS)
414                 goto done_free_sp;
415         return rval;
416
417 done_free_sp:
418         /* ref: INIT */
419         kref_put(&sp->cmd_kref, qla2x00_sp_release);
420 done:
421         fcport->flags &= ~(FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE);
422         return rval;
423 }
424
425 void
426 qla2x00_async_prlo_done(struct scsi_qla_host *vha, fc_port_t *fcport,
427     uint16_t *data)
428 {
429         fcport->flags &= ~FCF_ASYNC_ACTIVE;
430         /* Don't re-login in target mode */
431         if (!fcport->tgt_session)
432                 qla2x00_mark_device_lost(vha, fcport, 1);
433         qlt_logo_completion_handler(fcport, data[0]);
434 }
435
436 static void qla2x00_async_prlo_sp_done(srb_t *sp, int res)
437 {
438         struct srb_iocb *lio = &sp->u.iocb_cmd;
439         struct scsi_qla_host *vha = sp->vha;
440
441         sp->fcport->flags &= ~FCF_ASYNC_ACTIVE;
442         if (!test_bit(UNLOADING, &vha->dpc_flags))
443                 qla2x00_post_async_prlo_done_work(sp->fcport->vha, sp->fcport,
444                     lio->u.logio.data);
445         /* ref: INIT */
446         kref_put(&sp->cmd_kref, qla2x00_sp_release);
447 }
448
449 int
450 qla2x00_async_prlo(struct scsi_qla_host *vha, fc_port_t *fcport)
451 {
452         srb_t *sp;
453         int rval;
454
455         rval = QLA_FUNCTION_FAILED;
456         /* ref: INIT */
457         sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
458         if (!sp)
459                 goto done;
460
461         sp->type = SRB_PRLO_CMD;
462         sp->name = "prlo";
463         qla2x00_init_async_sp(sp, qla2x00_get_async_timeout(vha) + 2,
464                               qla2x00_async_prlo_sp_done);
465
466         ql_dbg(ql_dbg_disc, vha, 0x2070,
467             "Async-prlo - hdl=%x loop-id=%x portid=%02x%02x%02x.\n",
468             sp->handle, fcport->loop_id, fcport->d_id.b.domain,
469             fcport->d_id.b.area, fcport->d_id.b.al_pa);
470
471         rval = qla2x00_start_sp(sp);
472         if (rval != QLA_SUCCESS)
473                 goto done_free_sp;
474
475         return rval;
476
477 done_free_sp:
478         /* ref: INIT */
479         kref_put(&sp->cmd_kref, qla2x00_sp_release);
480 done:
481         fcport->flags &= ~FCF_ASYNC_ACTIVE;
482         return rval;
483 }
484
485 static
486 void qla24xx_handle_adisc_event(scsi_qla_host_t *vha, struct event_arg *ea)
487 {
488         struct fc_port *fcport = ea->fcport;
489
490         ql_dbg(ql_dbg_disc, vha, 0x20d2,
491             "%s %8phC DS %d LS %d rc %d login %d|%d rscn %d|%d lid %d\n",
492             __func__, fcport->port_name, fcport->disc_state,
493             fcport->fw_login_state, ea->rc, fcport->login_gen, ea->sp->gen2,
494             fcport->rscn_gen, ea->sp->gen1, fcport->loop_id);
495
496         WARN_ONCE(!qla2xxx_is_valid_mbs(ea->data[0]), "mbs: %#x\n",
497                   ea->data[0]);
498
499         if (ea->data[0] != MBS_COMMAND_COMPLETE) {
500                 ql_dbg(ql_dbg_disc, vha, 0x2066,
501                     "%s %8phC: adisc fail: post delete\n",
502                     __func__, ea->fcport->port_name);
503                 /* deleted = 0 & logout_on_delete = force fw cleanup */
504                 fcport->deleted = 0;
505                 fcport->logout_on_delete = 1;
506                 qlt_schedule_sess_for_deletion(ea->fcport);
507                 return;
508         }
509
510         if (ea->fcport->disc_state == DSC_DELETE_PEND)
511                 return;
512
513         if (ea->sp->gen2 != ea->fcport->login_gen) {
514                 /* target side must have changed it. */
515                 ql_dbg(ql_dbg_disc, vha, 0x20d3,
516                     "%s %8phC generation changed\n",
517                     __func__, ea->fcport->port_name);
518                 return;
519         } else if (ea->sp->gen1 != ea->fcport->rscn_gen) {
520                 qla_rscn_replay(fcport);
521                 qlt_schedule_sess_for_deletion(fcport);
522                 return;
523         }
524
525         __qla24xx_handle_gpdb_event(vha, ea);
526 }
527
528 static int qla_post_els_plogi_work(struct scsi_qla_host *vha, fc_port_t *fcport)
529 {
530         struct qla_work_evt *e;
531
532         e = qla2x00_alloc_work(vha, QLA_EVT_ELS_PLOGI);
533         if (!e)
534                 return QLA_FUNCTION_FAILED;
535
536         e->u.fcport.fcport = fcport;
537         fcport->flags |= FCF_ASYNC_ACTIVE;
538         qla2x00_set_fcport_disc_state(fcport, DSC_LOGIN_PEND);
539         return qla2x00_post_work(vha, e);
540 }
541
542 static void qla2x00_async_adisc_sp_done(srb_t *sp, int res)
543 {
544         struct scsi_qla_host *vha = sp->vha;
545         struct event_arg ea;
546         struct srb_iocb *lio = &sp->u.iocb_cmd;
547
548         ql_dbg(ql_dbg_disc, vha, 0x2066,
549             "Async done-%s res %x %8phC\n",
550             sp->name, res, sp->fcport->port_name);
551
552         sp->fcport->flags &= ~(FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE);
553
554         memset(&ea, 0, sizeof(ea));
555         ea.rc = res;
556         ea.data[0] = lio->u.logio.data[0];
557         ea.data[1] = lio->u.logio.data[1];
558         ea.iop[0] = lio->u.logio.iop[0];
559         ea.iop[1] = lio->u.logio.iop[1];
560         ea.fcport = sp->fcport;
561         ea.sp = sp;
562         if (res)
563                 ea.data[0] = MBS_COMMAND_ERROR;
564
565         qla24xx_handle_adisc_event(vha, &ea);
566         /* ref: INIT */
567         kref_put(&sp->cmd_kref, qla2x00_sp_release);
568 }
569
570 int
571 qla2x00_async_adisc(struct scsi_qla_host *vha, fc_port_t *fcport,
572     uint16_t *data)
573 {
574         srb_t *sp;
575         struct srb_iocb *lio;
576         int rval = QLA_FUNCTION_FAILED;
577
578         if (!vha->flags.online || (fcport->flags & FCF_ASYNC_SENT))
579                 return rval;
580
581         fcport->flags |= FCF_ASYNC_SENT;
582         /* ref: INIT */
583         sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
584         if (!sp)
585                 goto done;
586
587         sp->type = SRB_ADISC_CMD;
588         sp->name = "adisc";
589         sp->gen1 = fcport->rscn_gen;
590         sp->gen2 = fcport->login_gen;
591         qla2x00_init_async_sp(sp, qla2x00_get_async_timeout(vha) + 2,
592                               qla2x00_async_adisc_sp_done);
593
594         if (data[1] & QLA_LOGIO_LOGIN_RETRIED) {
595                 lio = &sp->u.iocb_cmd;
596                 lio->u.logio.flags |= SRB_LOGIN_RETRIED;
597         }
598
599         ql_dbg(ql_dbg_disc, vha, 0x206f,
600             "Async-adisc - hdl=%x loopid=%x portid=%06x %8phC.\n",
601             sp->handle, fcport->loop_id, fcport->d_id.b24, fcport->port_name);
602
603         rval = qla2x00_start_sp(sp);
604         if (rval != QLA_SUCCESS)
605                 goto done_free_sp;
606
607         return rval;
608
609 done_free_sp:
610         /* ref: INIT */
611         kref_put(&sp->cmd_kref, qla2x00_sp_release);
612 done:
613         fcport->flags &= ~(FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE);
614         qla2x00_post_async_adisc_work(vha, fcport, data);
615         return rval;
616 }
617
618 static bool qla2x00_is_reserved_id(scsi_qla_host_t *vha, uint16_t loop_id)
619 {
620         struct qla_hw_data *ha = vha->hw;
621
622         if (IS_FWI2_CAPABLE(ha))
623                 return loop_id > NPH_LAST_HANDLE;
624
625         return (loop_id > ha->max_loop_id && loop_id < SNS_FIRST_LOOP_ID) ||
626                 loop_id == MANAGEMENT_SERVER || loop_id == BROADCAST;
627 }
628
629 /**
630  * qla2x00_find_new_loop_id - scan through our port list and find a new usable loop ID
631  * @vha: adapter state pointer.
632  * @dev: port structure pointer.
633  *
634  * Returns:
635  *      qla2x00 local function return status code.
636  *
637  * Context:
638  *      Kernel context.
639  */
640 static int qla2x00_find_new_loop_id(scsi_qla_host_t *vha, fc_port_t *dev)
641 {
642         int     rval;
643         struct qla_hw_data *ha = vha->hw;
644         unsigned long flags = 0;
645
646         rval = QLA_SUCCESS;
647
648         spin_lock_irqsave(&ha->vport_slock, flags);
649
650         dev->loop_id = find_first_zero_bit(ha->loop_id_map, LOOPID_MAP_SIZE);
651         if (dev->loop_id >= LOOPID_MAP_SIZE ||
652             qla2x00_is_reserved_id(vha, dev->loop_id)) {
653                 dev->loop_id = FC_NO_LOOP_ID;
654                 rval = QLA_FUNCTION_FAILED;
655         } else {
656                 set_bit(dev->loop_id, ha->loop_id_map);
657         }
658         spin_unlock_irqrestore(&ha->vport_slock, flags);
659
660         if (rval == QLA_SUCCESS)
661                 ql_dbg(ql_dbg_disc, dev->vha, 0x2086,
662                        "Assigning new loopid=%x, portid=%x.\n",
663                        dev->loop_id, dev->d_id.b24);
664         else
665                 ql_log(ql_log_warn, dev->vha, 0x2087,
666                        "No loop_id's available, portid=%x.\n",
667                        dev->d_id.b24);
668
669         return rval;
670 }
671
672 void qla2x00_clear_loop_id(fc_port_t *fcport)
673 {
674         struct qla_hw_data *ha = fcport->vha->hw;
675
676         if (fcport->loop_id == FC_NO_LOOP_ID ||
677             qla2x00_is_reserved_id(fcport->vha, fcport->loop_id))
678                 return;
679
680         clear_bit(fcport->loop_id, ha->loop_id_map);
681         fcport->loop_id = FC_NO_LOOP_ID;
682 }
683
684 static void qla24xx_handle_gnl_done_event(scsi_qla_host_t *vha,
685         struct event_arg *ea)
686 {
687         fc_port_t *fcport, *conflict_fcport;
688         struct get_name_list_extended *e;
689         u16 i, n, found = 0, loop_id;
690         port_id_t id;
691         u64 wwn;
692         u16 data[2];
693         u8 current_login_state, nvme_cls;
694
695         fcport = ea->fcport;
696         ql_dbg(ql_dbg_disc, vha, 0xffff,
697             "%s %8phC DS %d LS rc %d %d login %d|%d rscn %d|%d lid %d edif %d\n",
698             __func__, fcport->port_name, fcport->disc_state,
699             fcport->fw_login_state, ea->rc,
700             fcport->login_gen, fcport->last_login_gen,
701             fcport->rscn_gen, fcport->last_rscn_gen, vha->loop_id, fcport->edif.enable);
702
703         if (fcport->disc_state == DSC_DELETE_PEND)
704                 return;
705
706         if (ea->rc) { /* rval */
707                 if (fcport->login_retry == 0) {
708                         ql_dbg(ql_dbg_disc, vha, 0x20de,
709                             "GNL failed Port login retry %8phN, retry cnt=%d.\n",
710                             fcport->port_name, fcport->login_retry);
711                 }
712                 return;
713         }
714
715         if (fcport->last_rscn_gen != fcport->rscn_gen) {
716                 qla_rscn_replay(fcport);
717                 qlt_schedule_sess_for_deletion(fcport);
718                 return;
719         } else if (fcport->last_login_gen != fcport->login_gen) {
720                 ql_dbg(ql_dbg_disc, vha, 0x20e0,
721                     "%s %8phC login gen changed\n",
722                     __func__, fcport->port_name);
723                 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
724                 return;
725         }
726
727         n = ea->data[0] / sizeof(struct get_name_list_extended);
728
729         ql_dbg(ql_dbg_disc, vha, 0x20e1,
730             "%s %d %8phC n %d %02x%02x%02x lid %d \n",
731             __func__, __LINE__, fcport->port_name, n,
732             fcport->d_id.b.domain, fcport->d_id.b.area,
733             fcport->d_id.b.al_pa, fcport->loop_id);
734
735         for (i = 0; i < n; i++) {
736                 e = &vha->gnl.l[i];
737                 wwn = wwn_to_u64(e->port_name);
738                 id.b.domain = e->port_id[2];
739                 id.b.area = e->port_id[1];
740                 id.b.al_pa = e->port_id[0];
741                 id.b.rsvd_1 = 0;
742
743                 if (memcmp((u8 *)&wwn, fcport->port_name, WWN_SIZE))
744                         continue;
745
746                 if (IS_SW_RESV_ADDR(id))
747                         continue;
748
749                 found = 1;
750
751                 loop_id = le16_to_cpu(e->nport_handle);
752                 loop_id = (loop_id & 0x7fff);
753                 nvme_cls = e->current_login_state >> 4;
754                 current_login_state = e->current_login_state & 0xf;
755
756                 if (PRLI_PHASE(nvme_cls)) {
757                         current_login_state = nvme_cls;
758                         fcport->fc4_type &= ~FS_FC4TYPE_FCP;
759                         fcport->fc4_type |= FS_FC4TYPE_NVME;
760                 } else if (PRLI_PHASE(current_login_state)) {
761                         fcport->fc4_type |= FS_FC4TYPE_FCP;
762                         fcport->fc4_type &= ~FS_FC4TYPE_NVME;
763                 }
764
765                 ql_dbg(ql_dbg_disc, vha, 0x20e2,
766                     "%s found %8phC CLS [%x|%x] fc4_type %d ID[%06x|%06x] lid[%d|%d]\n",
767                     __func__, fcport->port_name,
768                     e->current_login_state, fcport->fw_login_state,
769                     fcport->fc4_type, id.b24, fcport->d_id.b24,
770                     loop_id, fcport->loop_id);
771
772                 switch (fcport->disc_state) {
773                 case DSC_DELETE_PEND:
774                 case DSC_DELETED:
775                         break;
776                 default:
777                         if ((id.b24 != fcport->d_id.b24 &&
778                             fcport->d_id.b24 &&
779                             fcport->loop_id != FC_NO_LOOP_ID) ||
780                             (fcport->loop_id != FC_NO_LOOP_ID &&
781                                 fcport->loop_id != loop_id)) {
782                                 ql_dbg(ql_dbg_disc, vha, 0x20e3,
783                                     "%s %d %8phC post del sess\n",
784                                     __func__, __LINE__, fcport->port_name);
785                                 if (fcport->n2n_flag)
786                                         fcport->d_id.b24 = 0;
787                                 qlt_schedule_sess_for_deletion(fcport);
788                                 return;
789                         }
790                         break;
791                 }
792
793                 fcport->loop_id = loop_id;
794                 if (fcport->n2n_flag)
795                         fcport->d_id.b24 = id.b24;
796
797                 wwn = wwn_to_u64(fcport->port_name);
798                 qlt_find_sess_invalidate_other(vha, wwn,
799                         id, loop_id, &conflict_fcport);
800
801                 if (conflict_fcport) {
802                         /*
803                          * Another share fcport share the same loop_id &
804                          * nport id. Conflict fcport needs to finish
805                          * cleanup before this fcport can proceed to login.
806                          */
807                         conflict_fcport->conflict = fcport;
808                         fcport->login_pause = 1;
809                 }
810
811                 switch (vha->hw->current_topology) {
812                 default:
813                         switch (current_login_state) {
814                         case DSC_LS_PRLI_COMP:
815                                 ql_dbg(ql_dbg_disc,
816                                     vha, 0x20e4, "%s %d %8phC post gpdb\n",
817                                     __func__, __LINE__, fcport->port_name);
818
819                                 if ((e->prli_svc_param_word_3[0] & BIT_4) == 0)
820                                         fcport->port_type = FCT_INITIATOR;
821                                 else
822                                         fcport->port_type = FCT_TARGET;
823                                 data[0] = data[1] = 0;
824                                 qla2x00_post_async_adisc_work(vha, fcport,
825                                     data);
826                                 break;
827                         case DSC_LS_PLOGI_COMP:
828                                 if (vha->hw->flags.edif_enabled) {
829                                         /* check to see if App support Secure */
830                                         qla24xx_post_gpdb_work(vha, fcport, 0);
831                                         break;
832                                 }
833                                 fallthrough;
834                         case DSC_LS_PORT_UNAVAIL:
835                         default:
836                                 if (fcport->loop_id == FC_NO_LOOP_ID) {
837                                         qla2x00_find_new_loop_id(vha, fcport);
838                                         fcport->fw_login_state =
839                                             DSC_LS_PORT_UNAVAIL;
840                                 }
841                                 ql_dbg(ql_dbg_disc, vha, 0x20e5,
842                                     "%s %d %8phC\n", __func__, __LINE__,
843                                     fcport->port_name);
844                                 qla24xx_fcport_handle_login(vha, fcport);
845                                 break;
846                         }
847                         break;
848                 case ISP_CFG_N:
849                         fcport->fw_login_state = current_login_state;
850                         fcport->d_id = id;
851                         switch (current_login_state) {
852                         case DSC_LS_PRLI_PEND:
853                                 /*
854                                  * In the middle of PRLI. Let it finish.
855                                  * Allow relogin code to recheck state again
856                                  * with GNL. Push disc_state back to DELETED
857                                  * so GNL can go out again
858                                  */
859                                 qla2x00_set_fcport_disc_state(fcport,
860                                     DSC_DELETED);
861                                 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
862                                 break;
863                         case DSC_LS_PRLI_COMP:
864                                 if ((e->prli_svc_param_word_3[0] & BIT_4) == 0)
865                                         fcport->port_type = FCT_INITIATOR;
866                                 else
867                                         fcport->port_type = FCT_TARGET;
868
869                                 data[0] = data[1] = 0;
870                                 qla2x00_post_async_adisc_work(vha, fcport,
871                                     data);
872                                 break;
873                         case DSC_LS_PLOGI_COMP:
874                                 if (vha->hw->flags.edif_enabled &&
875                                     DBELL_ACTIVE(vha)) {
876                                         /* check to see if App support secure or not */
877                                         qla24xx_post_gpdb_work(vha, fcport, 0);
878                                         break;
879                                 }
880                                 if (fcport_is_bigger(fcport)) {
881                                         /* local adapter is smaller */
882                                         if (fcport->loop_id != FC_NO_LOOP_ID)
883                                                 qla2x00_clear_loop_id(fcport);
884
885                                         fcport->loop_id = loop_id;
886                                         qla24xx_fcport_handle_login(vha,
887                                             fcport);
888                                         break;
889                                 }
890                                 fallthrough;
891                         default:
892                                 if (fcport_is_smaller(fcport)) {
893                                         /* local adapter is bigger */
894                                         if (fcport->loop_id != FC_NO_LOOP_ID)
895                                                 qla2x00_clear_loop_id(fcport);
896
897                                         fcport->loop_id = loop_id;
898                                         qla24xx_fcport_handle_login(vha,
899                                             fcport);
900                                 }
901                                 break;
902                         }
903                         break;
904                 } /* switch (ha->current_topology) */
905         }
906
907         if (!found) {
908                 switch (vha->hw->current_topology) {
909                 case ISP_CFG_F:
910                 case ISP_CFG_FL:
911                         for (i = 0; i < n; i++) {
912                                 e = &vha->gnl.l[i];
913                                 id.b.domain = e->port_id[0];
914                                 id.b.area = e->port_id[1];
915                                 id.b.al_pa = e->port_id[2];
916                                 id.b.rsvd_1 = 0;
917                                 loop_id = le16_to_cpu(e->nport_handle);
918
919                                 if (fcport->d_id.b24 == id.b24) {
920                                         conflict_fcport =
921                                             qla2x00_find_fcport_by_wwpn(vha,
922                                                 e->port_name, 0);
923                                         if (conflict_fcport) {
924                                                 ql_dbg(ql_dbg_disc + ql_dbg_verbose,
925                                                     vha, 0x20e5,
926                                                     "%s %d %8phC post del sess\n",
927                                                     __func__, __LINE__,
928                                                     conflict_fcport->port_name);
929                                                 qlt_schedule_sess_for_deletion
930                                                         (conflict_fcport);
931                                         }
932                                 }
933                                 /*
934                                  * FW already picked this loop id for
935                                  * another fcport
936                                  */
937                                 if (fcport->loop_id == loop_id)
938                                         fcport->loop_id = FC_NO_LOOP_ID;
939                         }
940                         qla24xx_fcport_handle_login(vha, fcport);
941                         break;
942                 case ISP_CFG_N:
943                         qla2x00_set_fcport_disc_state(fcport, DSC_DELETED);
944                         if (time_after_eq(jiffies, fcport->dm_login_expire)) {
945                                 if (fcport->n2n_link_reset_cnt < 2) {
946                                         fcport->n2n_link_reset_cnt++;
947                                         /*
948                                          * remote port is not sending PLOGI.
949                                          * Reset link to kick start his state
950                                          * machine
951                                          */
952                                         set_bit(N2N_LINK_RESET,
953                                             &vha->dpc_flags);
954                                 } else {
955                                         if (fcport->n2n_chip_reset < 1) {
956                                                 ql_log(ql_log_info, vha, 0x705d,
957                                                     "Chip reset to bring laser down");
958                                                 set_bit(ISP_ABORT_NEEDED,
959                                                     &vha->dpc_flags);
960                                                 fcport->n2n_chip_reset++;
961                                         } else {
962                                                 ql_log(ql_log_info, vha, 0x705d,
963                                                     "Remote port %8ph is not coming back\n",
964                                                     fcport->port_name);
965                                                 fcport->scan_state = 0;
966                                         }
967                                 }
968                                 qla2xxx_wake_dpc(vha);
969                         } else {
970                                 /*
971                                  * report port suppose to do PLOGI. Give him
972                                  * more time. FW will catch it.
973                                  */
974                                 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
975                         }
976                         break;
977                 default:
978                         break;
979                 }
980         }
981 } /* gnl_event */
982
983 static void qla24xx_async_gnl_sp_done(srb_t *sp, int res)
984 {
985         struct scsi_qla_host *vha = sp->vha;
986         unsigned long flags;
987         struct fc_port *fcport = NULL, *tf;
988         u16 i, n = 0, loop_id;
989         struct event_arg ea;
990         struct get_name_list_extended *e;
991         u64 wwn;
992         struct list_head h;
993         bool found = false;
994
995         ql_dbg(ql_dbg_disc, vha, 0x20e7,
996             "Async done-%s res %x mb[1]=%x mb[2]=%x \n",
997             sp->name, res, sp->u.iocb_cmd.u.mbx.in_mb[1],
998             sp->u.iocb_cmd.u.mbx.in_mb[2]);
999
1000
1001         sp->fcport->flags &= ~(FCF_ASYNC_SENT|FCF_ASYNC_ACTIVE);
1002         memset(&ea, 0, sizeof(ea));
1003         ea.sp = sp;
1004         ea.rc = res;
1005
1006         if (sp->u.iocb_cmd.u.mbx.in_mb[1] >=
1007             sizeof(struct get_name_list_extended)) {
1008                 n = sp->u.iocb_cmd.u.mbx.in_mb[1] /
1009                     sizeof(struct get_name_list_extended);
1010                 ea.data[0] = sp->u.iocb_cmd.u.mbx.in_mb[1]; /* amnt xfered */
1011         }
1012
1013         for (i = 0; i < n; i++) {
1014                 e = &vha->gnl.l[i];
1015                 loop_id = le16_to_cpu(e->nport_handle);
1016                 /* mask out reserve bit */
1017                 loop_id = (loop_id & 0x7fff);
1018                 set_bit(loop_id, vha->hw->loop_id_map);
1019                 wwn = wwn_to_u64(e->port_name);
1020
1021                 ql_dbg(ql_dbg_disc, vha, 0x20e8,
1022                     "%s %8phC %02x:%02x:%02x CLS %x/%x lid %x \n",
1023                     __func__, &wwn, e->port_id[2], e->port_id[1],
1024                     e->port_id[0], e->current_login_state, e->last_login_state,
1025                     (loop_id & 0x7fff));
1026         }
1027
1028         spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
1029
1030         INIT_LIST_HEAD(&h);
1031         fcport = tf = NULL;
1032         if (!list_empty(&vha->gnl.fcports))
1033                 list_splice_init(&vha->gnl.fcports, &h);
1034         spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1035
1036         list_for_each_entry_safe(fcport, tf, &h, gnl_entry) {
1037                 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
1038                 list_del_init(&fcport->gnl_entry);
1039                 fcport->flags &= ~(FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE);
1040                 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1041                 ea.fcport = fcport;
1042
1043                 qla24xx_handle_gnl_done_event(vha, &ea);
1044         }
1045
1046         /* create new fcport if fw has knowledge of new sessions */
1047         for (i = 0; i < n; i++) {
1048                 port_id_t id;
1049                 u64 wwnn;
1050
1051                 e = &vha->gnl.l[i];
1052                 wwn = wwn_to_u64(e->port_name);
1053
1054                 found = false;
1055                 list_for_each_entry_safe(fcport, tf, &vha->vp_fcports, list) {
1056                         if (!memcmp((u8 *)&wwn, fcport->port_name,
1057                             WWN_SIZE)) {
1058                                 found = true;
1059                                 break;
1060                         }
1061                 }
1062
1063                 id.b.domain = e->port_id[2];
1064                 id.b.area = e->port_id[1];
1065                 id.b.al_pa = e->port_id[0];
1066                 id.b.rsvd_1 = 0;
1067
1068                 if (!found && wwn && !IS_SW_RESV_ADDR(id)) {
1069                         ql_dbg(ql_dbg_disc, vha, 0x2065,
1070                             "%s %d %8phC %06x post new sess\n",
1071                             __func__, __LINE__, (u8 *)&wwn, id.b24);
1072                         wwnn = wwn_to_u64(e->node_name);
1073                         qla24xx_post_newsess_work(vha, &id, (u8 *)&wwn,
1074                             (u8 *)&wwnn, NULL, 0);
1075                 }
1076         }
1077
1078         spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
1079         vha->gnl.sent = 0;
1080         if (!list_empty(&vha->gnl.fcports)) {
1081                 /* retrigger gnl */
1082                 list_for_each_entry_safe(fcport, tf, &vha->gnl.fcports,
1083                     gnl_entry) {
1084                         list_del_init(&fcport->gnl_entry);
1085                         fcport->flags &= ~(FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE);
1086                         if (qla24xx_post_gnl_work(vha, fcport) == QLA_SUCCESS)
1087                                 break;
1088                 }
1089         }
1090         spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1091
1092         /* ref: INIT */
1093         kref_put(&sp->cmd_kref, qla2x00_sp_release);
1094 }
1095
1096 int qla24xx_async_gnl(struct scsi_qla_host *vha, fc_port_t *fcport)
1097 {
1098         srb_t *sp;
1099         int rval = QLA_FUNCTION_FAILED;
1100         unsigned long flags;
1101         u16 *mb;
1102
1103         if (!vha->flags.online || (fcport->flags & FCF_ASYNC_SENT))
1104                 return rval;
1105
1106         ql_dbg(ql_dbg_disc, vha, 0x20d9,
1107             "Async-gnlist WWPN %8phC \n", fcport->port_name);
1108
1109         spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
1110         fcport->flags |= FCF_ASYNC_SENT;
1111         qla2x00_set_fcport_disc_state(fcport, DSC_GNL);
1112         fcport->last_rscn_gen = fcport->rscn_gen;
1113         fcport->last_login_gen = fcport->login_gen;
1114
1115         list_add_tail(&fcport->gnl_entry, &vha->gnl.fcports);
1116         if (vha->gnl.sent) {
1117                 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1118                 return QLA_SUCCESS;
1119         }
1120         vha->gnl.sent = 1;
1121         spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1122
1123         /* ref: INIT */
1124         sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
1125         if (!sp)
1126                 goto done;
1127
1128         sp->type = SRB_MB_IOCB;
1129         sp->name = "gnlist";
1130         sp->gen1 = fcport->rscn_gen;
1131         sp->gen2 = fcport->login_gen;
1132         qla2x00_init_async_sp(sp, qla2x00_get_async_timeout(vha) + 2,
1133                               qla24xx_async_gnl_sp_done);
1134
1135         mb = sp->u.iocb_cmd.u.mbx.out_mb;
1136         mb[0] = MBC_PORT_NODE_NAME_LIST;
1137         mb[1] = BIT_2 | BIT_3;
1138         mb[2] = MSW(vha->gnl.ldma);
1139         mb[3] = LSW(vha->gnl.ldma);
1140         mb[6] = MSW(MSD(vha->gnl.ldma));
1141         mb[7] = LSW(MSD(vha->gnl.ldma));
1142         mb[8] = vha->gnl.size;
1143         mb[9] = vha->vp_idx;
1144
1145         ql_dbg(ql_dbg_disc, vha, 0x20da,
1146             "Async-%s - OUT WWPN %8phC hndl %x\n",
1147             sp->name, fcport->port_name, sp->handle);
1148
1149         rval = qla2x00_start_sp(sp);
1150         if (rval != QLA_SUCCESS)
1151                 goto done_free_sp;
1152
1153         return rval;
1154
1155 done_free_sp:
1156         /* ref: INIT */
1157         kref_put(&sp->cmd_kref, qla2x00_sp_release);
1158 done:
1159         fcport->flags &= ~(FCF_ASYNC_ACTIVE | FCF_ASYNC_SENT);
1160         return rval;
1161 }
1162
1163 int qla24xx_post_gnl_work(struct scsi_qla_host *vha, fc_port_t *fcport)
1164 {
1165         struct qla_work_evt *e;
1166
1167         e = qla2x00_alloc_work(vha, QLA_EVT_GNL);
1168         if (!e)
1169                 return QLA_FUNCTION_FAILED;
1170
1171         e->u.fcport.fcport = fcport;
1172         fcport->flags |= FCF_ASYNC_ACTIVE;
1173         return qla2x00_post_work(vha, e);
1174 }
1175
1176 static void qla24xx_async_gpdb_sp_done(srb_t *sp, int res)
1177 {
1178         struct scsi_qla_host *vha = sp->vha;
1179         struct qla_hw_data *ha = vha->hw;
1180         fc_port_t *fcport = sp->fcport;
1181         u16 *mb = sp->u.iocb_cmd.u.mbx.in_mb;
1182         struct event_arg ea;
1183
1184         ql_dbg(ql_dbg_disc, vha, 0x20db,
1185             "Async done-%s res %x, WWPN %8phC mb[1]=%x mb[2]=%x \n",
1186             sp->name, res, fcport->port_name, mb[1], mb[2]);
1187
1188         fcport->flags &= ~(FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE);
1189
1190         if (res == QLA_FUNCTION_TIMEOUT)
1191                 goto done;
1192
1193         memset(&ea, 0, sizeof(ea));
1194         ea.fcport = fcport;
1195         ea.sp = sp;
1196
1197         qla24xx_handle_gpdb_event(vha, &ea);
1198
1199 done:
1200         dma_pool_free(ha->s_dma_pool, sp->u.iocb_cmd.u.mbx.in,
1201                 sp->u.iocb_cmd.u.mbx.in_dma);
1202
1203         kref_put(&sp->cmd_kref, qla2x00_sp_release);
1204 }
1205
1206 int qla24xx_post_prli_work(struct scsi_qla_host *vha, fc_port_t *fcport)
1207 {
1208         struct qla_work_evt *e;
1209
1210         if (vha->host->active_mode == MODE_TARGET)
1211                 return QLA_FUNCTION_FAILED;
1212
1213         e = qla2x00_alloc_work(vha, QLA_EVT_PRLI);
1214         if (!e)
1215                 return QLA_FUNCTION_FAILED;
1216
1217         e->u.fcport.fcport = fcport;
1218
1219         return qla2x00_post_work(vha, e);
1220 }
1221
1222 static void qla2x00_async_prli_sp_done(srb_t *sp, int res)
1223 {
1224         struct scsi_qla_host *vha = sp->vha;
1225         struct srb_iocb *lio = &sp->u.iocb_cmd;
1226         struct event_arg ea;
1227
1228         ql_dbg(ql_dbg_disc, vha, 0x2129,
1229             "%s %8phC res %x\n", __func__,
1230             sp->fcport->port_name, res);
1231
1232         sp->fcport->flags &= ~FCF_ASYNC_SENT;
1233
1234         if (!test_bit(UNLOADING, &vha->dpc_flags)) {
1235                 memset(&ea, 0, sizeof(ea));
1236                 ea.fcport = sp->fcport;
1237                 ea.data[0] = lio->u.logio.data[0];
1238                 ea.data[1] = lio->u.logio.data[1];
1239                 ea.iop[0] = lio->u.logio.iop[0];
1240                 ea.iop[1] = lio->u.logio.iop[1];
1241                 ea.sp = sp;
1242                 if (res == QLA_OS_TIMER_EXPIRED)
1243                         ea.data[0] = QLA_OS_TIMER_EXPIRED;
1244                 else if (res)
1245                         ea.data[0] = MBS_COMMAND_ERROR;
1246
1247                 qla24xx_handle_prli_done_event(vha, &ea);
1248         }
1249
1250         kref_put(&sp->cmd_kref, qla2x00_sp_release);
1251 }
1252
1253 int
1254 qla24xx_async_prli(struct scsi_qla_host *vha, fc_port_t *fcport)
1255 {
1256         srb_t *sp;
1257         struct srb_iocb *lio;
1258         int rval = QLA_FUNCTION_FAILED;
1259
1260         if (!vha->flags.online) {
1261                 ql_dbg(ql_dbg_disc, vha, 0xffff, "%s %d %8phC exit\n",
1262                     __func__, __LINE__, fcport->port_name);
1263                 return rval;
1264         }
1265
1266         if ((fcport->fw_login_state == DSC_LS_PLOGI_PEND ||
1267             fcport->fw_login_state == DSC_LS_PRLI_PEND) &&
1268             qla_dual_mode_enabled(vha)) {
1269                 ql_dbg(ql_dbg_disc, vha, 0xffff, "%s %d %8phC exit\n",
1270                     __func__, __LINE__, fcport->port_name);
1271                 return rval;
1272         }
1273
1274         sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
1275         if (!sp)
1276                 return rval;
1277
1278         fcport->flags |= FCF_ASYNC_SENT;
1279         fcport->logout_completed = 0;
1280
1281         sp->type = SRB_PRLI_CMD;
1282         sp->name = "prli";
1283         qla2x00_init_async_sp(sp, qla2x00_get_async_timeout(vha) + 2,
1284                               qla2x00_async_prli_sp_done);
1285
1286         lio = &sp->u.iocb_cmd;
1287         lio->u.logio.flags = 0;
1288
1289         if (NVME_TARGET(vha->hw, fcport))
1290                 lio->u.logio.flags |= SRB_LOGIN_NVME_PRLI;
1291
1292         ql_dbg(ql_dbg_disc, vha, 0x211b,
1293             "Async-prli - %8phC hdl=%x, loopid=%x portid=%06x retries=%d fc4type %x priority %x %s.\n",
1294             fcport->port_name, sp->handle, fcport->loop_id, fcport->d_id.b24,
1295             fcport->login_retry, fcport->fc4_type, vha->hw->fc4_type_priority,
1296             NVME_TARGET(vha->hw, fcport) ? "nvme" : "fcp");
1297
1298         rval = qla2x00_start_sp(sp);
1299         if (rval != QLA_SUCCESS) {
1300                 fcport->flags |= FCF_LOGIN_NEEDED;
1301                 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1302                 goto done_free_sp;
1303         }
1304
1305         return rval;
1306
1307 done_free_sp:
1308         /* ref: INIT */
1309         kref_put(&sp->cmd_kref, qla2x00_sp_release);
1310         fcport->flags &= ~FCF_ASYNC_SENT;
1311         return rval;
1312 }
1313
1314 int qla24xx_post_gpdb_work(struct scsi_qla_host *vha, fc_port_t *fcport, u8 opt)
1315 {
1316         struct qla_work_evt *e;
1317
1318         e = qla2x00_alloc_work(vha, QLA_EVT_GPDB);
1319         if (!e)
1320                 return QLA_FUNCTION_FAILED;
1321
1322         e->u.fcport.fcport = fcport;
1323         e->u.fcport.opt = opt;
1324         fcport->flags |= FCF_ASYNC_ACTIVE;
1325         return qla2x00_post_work(vha, e);
1326 }
1327
1328 int qla24xx_async_gpdb(struct scsi_qla_host *vha, fc_port_t *fcport, u8 opt)
1329 {
1330         srb_t *sp;
1331         struct srb_iocb *mbx;
1332         int rval = QLA_FUNCTION_FAILED;
1333         u16 *mb;
1334         dma_addr_t pd_dma;
1335         struct port_database_24xx *pd;
1336         struct qla_hw_data *ha = vha->hw;
1337
1338         if (!vha->flags.online || (fcport->flags & FCF_ASYNC_SENT) ||
1339             fcport->loop_id == FC_NO_LOOP_ID) {
1340                 ql_log(ql_log_warn, vha, 0xffff,
1341                     "%s: %8phC online %d flags %x - not sending command.\n",
1342                     __func__, fcport->port_name, vha->flags.online, fcport->flags);
1343                 goto done;
1344         }
1345
1346         sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
1347         if (!sp)
1348                 goto done;
1349
1350         qla2x00_set_fcport_disc_state(fcport, DSC_GPDB);
1351
1352         fcport->flags |= FCF_ASYNC_SENT;
1353         sp->type = SRB_MB_IOCB;
1354         sp->name = "gpdb";
1355         sp->gen1 = fcport->rscn_gen;
1356         sp->gen2 = fcport->login_gen;
1357         qla2x00_init_async_sp(sp, qla2x00_get_async_timeout(vha) + 2,
1358                               qla24xx_async_gpdb_sp_done);
1359
1360         pd = dma_pool_zalloc(ha->s_dma_pool, GFP_KERNEL, &pd_dma);
1361         if (pd == NULL) {
1362                 ql_log(ql_log_warn, vha, 0xd043,
1363                     "Failed to allocate port database structure.\n");
1364                 goto done_free_sp;
1365         }
1366
1367         mb = sp->u.iocb_cmd.u.mbx.out_mb;
1368         mb[0] = MBC_GET_PORT_DATABASE;
1369         mb[1] = fcport->loop_id;
1370         mb[2] = MSW(pd_dma);
1371         mb[3] = LSW(pd_dma);
1372         mb[6] = MSW(MSD(pd_dma));
1373         mb[7] = LSW(MSD(pd_dma));
1374         mb[9] = vha->vp_idx;
1375         mb[10] = opt;
1376
1377         mbx = &sp->u.iocb_cmd;
1378         mbx->u.mbx.in = (void *)pd;
1379         mbx->u.mbx.in_dma = pd_dma;
1380
1381         ql_dbg(ql_dbg_disc, vha, 0x20dc,
1382             "Async-%s %8phC hndl %x opt %x\n",
1383             sp->name, fcport->port_name, sp->handle, opt);
1384
1385         rval = qla2x00_start_sp(sp);
1386         if (rval != QLA_SUCCESS)
1387                 goto done_free_sp;
1388         return rval;
1389
1390 done_free_sp:
1391         if (pd)
1392                 dma_pool_free(ha->s_dma_pool, pd, pd_dma);
1393
1394         kref_put(&sp->cmd_kref, qla2x00_sp_release);
1395         fcport->flags &= ~FCF_ASYNC_SENT;
1396 done:
1397         fcport->flags &= ~FCF_ASYNC_ACTIVE;
1398         qla24xx_post_gpdb_work(vha, fcport, opt);
1399         return rval;
1400 }
1401
1402 static
1403 void __qla24xx_handle_gpdb_event(scsi_qla_host_t *vha, struct event_arg *ea)
1404 {
1405         unsigned long flags;
1406
1407         spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
1408         ea->fcport->login_gen++;
1409         ea->fcport->deleted = 0;
1410         ea->fcport->logout_on_delete = 1;
1411
1412         if (!ea->fcport->login_succ && !IS_SW_RESV_ADDR(ea->fcport->d_id)) {
1413                 vha->fcport_count++;
1414                 ea->fcport->login_succ = 1;
1415
1416                 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1417                 qla24xx_sched_upd_fcport(ea->fcport);
1418                 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
1419         } else if (ea->fcport->login_succ) {
1420                 /*
1421                  * We have an existing session. A late RSCN delivery
1422                  * must have triggered the session to be re-validate.
1423                  * Session is still valid.
1424                  */
1425                 ql_dbg(ql_dbg_disc, vha, 0x20d6,
1426                     "%s %d %8phC session revalidate success\n",
1427                     __func__, __LINE__, ea->fcport->port_name);
1428                 qla2x00_set_fcport_disc_state(ea->fcport, DSC_LOGIN_COMPLETE);
1429         }
1430         spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1431 }
1432
1433 static int      qla_chk_secure_login(scsi_qla_host_t    *vha, fc_port_t *fcport,
1434         struct port_database_24xx *pd)
1435 {
1436         int rc = 0;
1437
1438         if (pd->secure_login) {
1439                 ql_dbg(ql_dbg_disc, vha, 0x104d,
1440                     "Secure Login established on %8phC\n",
1441                     fcport->port_name);
1442                 fcport->flags |= FCF_FCSP_DEVICE;
1443         } else {
1444                 ql_dbg(ql_dbg_disc, vha, 0x104d,
1445                     "non-Secure Login %8phC",
1446                     fcport->port_name);
1447                 fcport->flags &= ~FCF_FCSP_DEVICE;
1448         }
1449         if (vha->hw->flags.edif_enabled) {
1450                 if (fcport->flags & FCF_FCSP_DEVICE) {
1451                         qla2x00_set_fcport_disc_state(fcport, DSC_LOGIN_AUTH_PEND);
1452                         /* Start edif prli timer & ring doorbell for app */
1453                         fcport->edif.rx_sa_set = 0;
1454                         fcport->edif.tx_sa_set = 0;
1455                         fcport->edif.rx_sa_pending = 0;
1456                         fcport->edif.tx_sa_pending = 0;
1457
1458                         qla2x00_post_aen_work(vha, FCH_EVT_PORT_ONLINE,
1459                             fcport->d_id.b24);
1460
1461                         if (DBELL_ACTIVE(vha)) {
1462                                 ql_dbg(ql_dbg_disc, vha, 0x20ef,
1463                                     "%s %d %8phC EDIF: post DB_AUTH: AUTH needed\n",
1464                                     __func__, __LINE__, fcport->port_name);
1465                                 fcport->edif.app_started = 1;
1466                                 fcport->edif.app_sess_online = 1;
1467
1468                                 qla_edb_eventcreate(vha, VND_CMD_AUTH_STATE_NEEDED,
1469                                     fcport->d_id.b24, 0, fcport);
1470                         }
1471
1472                         rc = 1;
1473                 } else if (qla_ini_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
1474                         ql_dbg(ql_dbg_disc, vha, 0x2117,
1475                             "%s %d %8phC post prli\n",
1476                             __func__, __LINE__, fcport->port_name);
1477                         qla24xx_post_prli_work(vha, fcport);
1478                         rc = 1;
1479                 }
1480         }
1481         return rc;
1482 }
1483
1484 static
1485 void qla24xx_handle_gpdb_event(scsi_qla_host_t *vha, struct event_arg *ea)
1486 {
1487         fc_port_t *fcport = ea->fcport;
1488         struct port_database_24xx *pd;
1489         struct srb *sp = ea->sp;
1490         uint8_t ls;
1491
1492         pd = (struct port_database_24xx *)sp->u.iocb_cmd.u.mbx.in;
1493
1494         fcport->flags &= ~FCF_ASYNC_SENT;
1495
1496         ql_dbg(ql_dbg_disc, vha, 0x20d2,
1497             "%s %8phC DS %d LS %x fc4_type %x rc %x\n", __func__,
1498             fcport->port_name, fcport->disc_state, pd->current_login_state,
1499             fcport->fc4_type, ea->rc);
1500
1501         if (fcport->disc_state == DSC_DELETE_PEND) {
1502                 ql_dbg(ql_dbg_disc, vha, 0x20d5, "%s %d %8phC\n",
1503                        __func__, __LINE__, fcport->port_name);
1504                 return;
1505         }
1506
1507         if (NVME_TARGET(vha->hw, fcport))
1508                 ls = pd->current_login_state >> 4;
1509         else
1510                 ls = pd->current_login_state & 0xf;
1511
1512         if (ea->sp->gen2 != fcport->login_gen) {
1513                 /* target side must have changed it. */
1514
1515                 ql_dbg(ql_dbg_disc, vha, 0x20d3,
1516                     "%s %8phC generation changed\n",
1517                     __func__, fcport->port_name);
1518                 return;
1519         } else if (ea->sp->gen1 != fcport->rscn_gen) {
1520                 qla_rscn_replay(fcport);
1521                 qlt_schedule_sess_for_deletion(fcport);
1522                 ql_dbg(ql_dbg_disc, vha, 0x20d5, "%s %d %8phC, ls %x\n",
1523                        __func__, __LINE__, fcport->port_name, ls);
1524                 return;
1525         }
1526
1527         switch (ls) {
1528         case PDS_PRLI_COMPLETE:
1529                 __qla24xx_parse_gpdb(vha, fcport, pd);
1530                 break;
1531         case PDS_PLOGI_COMPLETE:
1532                 if (qla_chk_secure_login(vha, fcport, pd)) {
1533                         ql_dbg(ql_dbg_disc, vha, 0x20d5, "%s %d %8phC, ls %x\n",
1534                                __func__, __LINE__, fcport->port_name, ls);
1535                         return;
1536                 }
1537                 fallthrough;
1538         case PDS_PLOGI_PENDING:
1539         case PDS_PRLI_PENDING:
1540         case PDS_PRLI2_PENDING:
1541                 /* Set discovery state back to GNL to Relogin attempt */
1542                 if (qla_dual_mode_enabled(vha) ||
1543                     qla_ini_mode_enabled(vha)) {
1544                         qla2x00_set_fcport_disc_state(fcport, DSC_GNL);
1545                         set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1546                 }
1547                 ql_dbg(ql_dbg_disc, vha, 0x20d5, "%s %d %8phC, ls %x\n",
1548                        __func__, __LINE__, fcport->port_name, ls);
1549                 return;
1550         case PDS_LOGO_PENDING:
1551         case PDS_PORT_UNAVAILABLE:
1552         default:
1553                 ql_dbg(ql_dbg_disc, vha, 0x20d5, "%s %d %8phC post del sess\n",
1554                     __func__, __LINE__, fcport->port_name);
1555                 qlt_schedule_sess_for_deletion(fcport);
1556                 return;
1557         }
1558         __qla24xx_handle_gpdb_event(vha, ea);
1559 } /* gpdb event */
1560
1561 static void qla_chk_n2n_b4_login(struct scsi_qla_host *vha, fc_port_t *fcport)
1562 {
1563         u8 login = 0;
1564         int rc;
1565
1566         if (qla_tgt_mode_enabled(vha))
1567                 return;
1568
1569         if (qla_dual_mode_enabled(vha)) {
1570                 if (N2N_TOPO(vha->hw)) {
1571                         u64 mywwn, wwn;
1572
1573                         mywwn = wwn_to_u64(vha->port_name);
1574                         wwn = wwn_to_u64(fcport->port_name);
1575                         if (mywwn > wwn)
1576                                 login = 1;
1577                         else if ((fcport->fw_login_state == DSC_LS_PLOGI_COMP)
1578                             && time_after_eq(jiffies,
1579                                     fcport->plogi_nack_done_deadline))
1580                                 login = 1;
1581                 } else {
1582                         login = 1;
1583                 }
1584         } else {
1585                 /* initiator mode */
1586                 login = 1;
1587         }
1588
1589         if (login && fcport->login_retry) {
1590                 fcport->login_retry--;
1591                 if (fcport->loop_id == FC_NO_LOOP_ID) {
1592                         fcport->fw_login_state = DSC_LS_PORT_UNAVAIL;
1593                         rc = qla2x00_find_new_loop_id(vha, fcport);
1594                         if (rc) {
1595                                 ql_dbg(ql_dbg_disc, vha, 0x20e6,
1596                                     "%s %d %8phC post del sess - out of loopid\n",
1597                                     __func__, __LINE__, fcport->port_name);
1598                                 fcport->scan_state = 0;
1599                                 qlt_schedule_sess_for_deletion(fcport);
1600                                 return;
1601                         }
1602                 }
1603                 ql_dbg(ql_dbg_disc, vha, 0x20bf,
1604                     "%s %d %8phC post login\n",
1605                     __func__, __LINE__, fcport->port_name);
1606                 qla2x00_post_async_login_work(vha, fcport, NULL);
1607         }
1608 }
1609
1610 int qla24xx_fcport_handle_login(struct scsi_qla_host *vha, fc_port_t *fcport)
1611 {
1612         u16 data[2];
1613         u64 wwn;
1614         u16 sec;
1615
1616         ql_dbg(ql_dbg_disc, vha, 0x20d8,
1617             "%s %8phC DS %d LS %d P %d fl %x confl %p rscn %d|%d login %d lid %d scan %d fc4type %x\n",
1618             __func__, fcport->port_name, fcport->disc_state,
1619             fcport->fw_login_state, fcport->login_pause, fcport->flags,
1620             fcport->conflict, fcport->last_rscn_gen, fcport->rscn_gen,
1621             fcport->login_gen, fcport->loop_id, fcport->scan_state,
1622             fcport->fc4_type);
1623
1624         if (fcport->scan_state != QLA_FCPORT_FOUND)
1625                 return 0;
1626
1627         if ((fcport->loop_id != FC_NO_LOOP_ID) &&
1628             qla_dual_mode_enabled(vha) &&
1629             ((fcport->fw_login_state == DSC_LS_PLOGI_PEND) ||
1630              (fcport->fw_login_state == DSC_LS_PRLI_PEND)))
1631                 return 0;
1632
1633         if (fcport->fw_login_state == DSC_LS_PLOGI_COMP &&
1634             !N2N_TOPO(vha->hw)) {
1635                 if (time_before_eq(jiffies, fcport->plogi_nack_done_deadline)) {
1636                         set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1637                         return 0;
1638                 }
1639         }
1640
1641         /* Target won't initiate port login if fabric is present */
1642         if (vha->host->active_mode == MODE_TARGET && !N2N_TOPO(vha->hw))
1643                 return 0;
1644
1645         if (fcport->flags & FCF_ASYNC_SENT) {
1646                 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1647                 return 0;
1648         }
1649
1650         switch (fcport->disc_state) {
1651         case DSC_DELETED:
1652                 wwn = wwn_to_u64(fcport->node_name);
1653                 switch (vha->hw->current_topology) {
1654                 case ISP_CFG_N:
1655                         if (fcport_is_smaller(fcport)) {
1656                                 /* this adapter is bigger */
1657                                 if (fcport->login_retry) {
1658                                         if (fcport->loop_id == FC_NO_LOOP_ID) {
1659                                                 qla2x00_find_new_loop_id(vha,
1660                                                     fcport);
1661                                                 fcport->fw_login_state =
1662                                                     DSC_LS_PORT_UNAVAIL;
1663                                         }
1664                                         fcport->login_retry--;
1665                                         qla_post_els_plogi_work(vha, fcport);
1666                                 } else {
1667                                         ql_log(ql_log_info, vha, 0x705d,
1668                                             "Unable to reach remote port %8phC",
1669                                             fcport->port_name);
1670                                 }
1671                         } else {
1672                                 qla24xx_post_gnl_work(vha, fcport);
1673                         }
1674                         break;
1675                 default:
1676                         if (wwn == 0)    {
1677                                 ql_dbg(ql_dbg_disc, vha, 0xffff,
1678                                     "%s %d %8phC post GNNID\n",
1679                                     __func__, __LINE__, fcport->port_name);
1680                                 qla24xx_post_gnnid_work(vha, fcport);
1681                         } else if (fcport->loop_id == FC_NO_LOOP_ID) {
1682                                 ql_dbg(ql_dbg_disc, vha, 0x20bd,
1683                                     "%s %d %8phC post gnl\n",
1684                                     __func__, __LINE__, fcport->port_name);
1685                                 qla24xx_post_gnl_work(vha, fcport);
1686                         } else {
1687                                 qla_chk_n2n_b4_login(vha, fcport);
1688                         }
1689                         break;
1690                 }
1691                 break;
1692
1693         case DSC_GNL:
1694                 switch (vha->hw->current_topology) {
1695                 case ISP_CFG_N:
1696                         if ((fcport->current_login_state & 0xf) == 0x6) {
1697                                 ql_dbg(ql_dbg_disc, vha, 0x2118,
1698                                     "%s %d %8phC post GPDB work\n",
1699                                     __func__, __LINE__, fcport->port_name);
1700                                 fcport->chip_reset =
1701                                         vha->hw->base_qpair->chip_reset;
1702                                 qla24xx_post_gpdb_work(vha, fcport, 0);
1703                         }  else {
1704                                 ql_dbg(ql_dbg_disc, vha, 0x2118,
1705                                     "%s %d %8phC post %s PRLI\n",
1706                                     __func__, __LINE__, fcport->port_name,
1707                                     NVME_TARGET(vha->hw, fcport) ? "NVME" :
1708                                     "FC");
1709                                 qla24xx_post_prli_work(vha, fcport);
1710                         }
1711                         break;
1712                 default:
1713                         if (fcport->login_pause) {
1714                                 ql_dbg(ql_dbg_disc, vha, 0x20d8,
1715                                     "%s %d %8phC exit\n",
1716                                     __func__, __LINE__,
1717                                     fcport->port_name);
1718                                 fcport->last_rscn_gen = fcport->rscn_gen;
1719                                 fcport->last_login_gen = fcport->login_gen;
1720                                 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1721                                 break;
1722                         }
1723                         qla_chk_n2n_b4_login(vha, fcport);
1724                         break;
1725                 }
1726                 break;
1727
1728         case DSC_LOGIN_FAILED:
1729                 if (N2N_TOPO(vha->hw))
1730                         qla_chk_n2n_b4_login(vha, fcport);
1731                 else
1732                         qlt_schedule_sess_for_deletion(fcport);
1733                 break;
1734
1735         case DSC_LOGIN_COMPLETE:
1736                 /* recheck login state */
1737                 data[0] = data[1] = 0;
1738                 qla2x00_post_async_adisc_work(vha, fcport, data);
1739                 break;
1740
1741         case DSC_LOGIN_PEND:
1742                 if (fcport->fw_login_state == DSC_LS_PLOGI_COMP)
1743                         qla24xx_post_prli_work(vha, fcport);
1744                 break;
1745
1746         case DSC_UPD_FCPORT:
1747                 sec =  jiffies_to_msecs(jiffies -
1748                     fcport->jiffies_at_registration)/1000;
1749                 if (fcport->sec_since_registration < sec && sec &&
1750                     !(sec % 60)) {
1751                         fcport->sec_since_registration = sec;
1752                         ql_dbg(ql_dbg_disc, fcport->vha, 0xffff,
1753                             "%s %8phC - Slow Rport registration(%d Sec)\n",
1754                             __func__, fcport->port_name, sec);
1755                 }
1756
1757                 if (fcport->next_disc_state != DSC_DELETE_PEND)
1758                         fcport->next_disc_state = DSC_ADISC;
1759                 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1760                 break;
1761
1762         default:
1763                 break;
1764         }
1765
1766         return 0;
1767 }
1768
1769 int qla24xx_post_newsess_work(struct scsi_qla_host *vha, port_id_t *id,
1770     u8 *port_name, u8 *node_name, void *pla, u8 fc4_type)
1771 {
1772         struct qla_work_evt *e;
1773
1774         e = qla2x00_alloc_work(vha, QLA_EVT_NEW_SESS);
1775         if (!e)
1776                 return QLA_FUNCTION_FAILED;
1777
1778         e->u.new_sess.id = *id;
1779         e->u.new_sess.pla = pla;
1780         e->u.new_sess.fc4_type = fc4_type;
1781         memcpy(e->u.new_sess.port_name, port_name, WWN_SIZE);
1782         if (node_name)
1783                 memcpy(e->u.new_sess.node_name, node_name, WWN_SIZE);
1784
1785         return qla2x00_post_work(vha, e);
1786 }
1787
1788 void qla2x00_handle_rscn(scsi_qla_host_t *vha, struct event_arg *ea)
1789 {
1790         fc_port_t *fcport;
1791         unsigned long flags;
1792
1793         switch (ea->id.b.rsvd_1) {
1794         case RSCN_PORT_ADDR:
1795                 fcport = qla2x00_find_fcport_by_nportid(vha, &ea->id, 1);
1796                 if (fcport) {
1797                         if (fcport->flags & FCF_FCP2_DEVICE) {
1798                                 ql_dbg(ql_dbg_disc, vha, 0x2115,
1799                                        "Delaying session delete for FCP2 portid=%06x %8phC ",
1800                                         fcport->d_id.b24, fcport->port_name);
1801                                 return;
1802                         }
1803
1804                         if (vha->hw->flags.edif_enabled && DBELL_ACTIVE(vha)) {
1805                                 /*
1806                                  * On ipsec start by remote port, Target port
1807                                  * may use RSCN to trigger initiator to
1808                                  * relogin. If driver is already in the
1809                                  * process of a relogin, then ignore the RSCN
1810                                  * and allow the current relogin to continue.
1811                                  * This reduces thrashing of the connection.
1812                                  */
1813                                 if (atomic_read(&fcport->state) == FCS_ONLINE) {
1814                                         /*
1815                                          * If state = online, then set scan_needed=1 to do relogin.
1816                                          * Otherwise we're already in the middle of a relogin
1817                                          */
1818                                         fcport->scan_needed = 1;
1819                                         fcport->rscn_gen++;
1820                                 }
1821                         } else {
1822                                 fcport->scan_needed = 1;
1823                                 fcport->rscn_gen++;
1824                         }
1825                 }
1826                 break;
1827         case RSCN_AREA_ADDR:
1828                 list_for_each_entry(fcport, &vha->vp_fcports, list) {
1829                         if (fcport->flags & FCF_FCP2_DEVICE)
1830                                 continue;
1831
1832                         if ((ea->id.b24 & 0xffff00) == (fcport->d_id.b24 & 0xffff00)) {
1833                                 fcport->scan_needed = 1;
1834                                 fcport->rscn_gen++;
1835                         }
1836                 }
1837                 break;
1838         case RSCN_DOM_ADDR:
1839                 list_for_each_entry(fcport, &vha->vp_fcports, list) {
1840                         if (fcport->flags & FCF_FCP2_DEVICE)
1841                                 continue;
1842
1843                         if ((ea->id.b24 & 0xff0000) == (fcport->d_id.b24 & 0xff0000)) {
1844                                 fcport->scan_needed = 1;
1845                                 fcport->rscn_gen++;
1846                         }
1847                 }
1848                 break;
1849         case RSCN_FAB_ADDR:
1850         default:
1851                 list_for_each_entry(fcport, &vha->vp_fcports, list) {
1852                         if (fcport->flags & FCF_FCP2_DEVICE)
1853                                 continue;
1854
1855                         fcport->scan_needed = 1;
1856                         fcport->rscn_gen++;
1857                 }
1858                 break;
1859         }
1860
1861         spin_lock_irqsave(&vha->work_lock, flags);
1862         if (vha->scan.scan_flags == 0) {
1863                 ql_dbg(ql_dbg_disc, vha, 0xffff, "%s: schedule\n", __func__);
1864                 vha->scan.scan_flags |= SF_QUEUED;
1865                 schedule_delayed_work(&vha->scan.scan_work, 5);
1866         }
1867         spin_unlock_irqrestore(&vha->work_lock, flags);
1868 }
1869
1870 void qla24xx_handle_relogin_event(scsi_qla_host_t *vha,
1871         struct event_arg *ea)
1872 {
1873         fc_port_t *fcport = ea->fcport;
1874
1875         if (test_bit(UNLOADING, &vha->dpc_flags))
1876                 return;
1877
1878         ql_dbg(ql_dbg_disc, vha, 0x2102,
1879             "%s %8phC DS %d LS %d P %d del %d cnfl %p rscn %d|%d login %d|%d fl %x\n",
1880             __func__, fcport->port_name, fcport->disc_state,
1881             fcport->fw_login_state, fcport->login_pause,
1882             fcport->deleted, fcport->conflict,
1883             fcport->last_rscn_gen, fcport->rscn_gen,
1884             fcport->last_login_gen, fcport->login_gen,
1885             fcport->flags);
1886
1887         if (fcport->last_rscn_gen != fcport->rscn_gen) {
1888                 ql_dbg(ql_dbg_disc, vha, 0x20e9, "%s %d %8phC post gnl\n",
1889                     __func__, __LINE__, fcport->port_name);
1890                 qla24xx_post_gnl_work(vha, fcport);
1891                 return;
1892         }
1893
1894         qla24xx_fcport_handle_login(vha, fcport);
1895 }
1896
1897 void qla_handle_els_plogi_done(scsi_qla_host_t *vha,
1898                                       struct event_arg *ea)
1899 {
1900         if (N2N_TOPO(vha->hw) && fcport_is_smaller(ea->fcport) &&
1901             vha->hw->flags.edif_enabled) {
1902                 /* check to see if App support Secure */
1903                 qla24xx_post_gpdb_work(vha, ea->fcport, 0);
1904                 return;
1905         }
1906
1907         /* for pure Target Mode, PRLI will not be initiated */
1908         if (vha->host->active_mode == MODE_TARGET)
1909                 return;
1910
1911         ql_dbg(ql_dbg_disc, vha, 0x2118,
1912             "%s %d %8phC post PRLI\n",
1913             __func__, __LINE__, ea->fcport->port_name);
1914         qla24xx_post_prli_work(vha, ea->fcport);
1915 }
1916
1917 /*
1918  * RSCN(s) came in for this fcport, but the RSCN(s) was not able
1919  * to be consumed by the fcport
1920  */
1921 void qla_rscn_replay(fc_port_t *fcport)
1922 {
1923         struct event_arg ea;
1924
1925         switch (fcport->disc_state) {
1926         case DSC_DELETE_PEND:
1927                 return;
1928         default:
1929                 break;
1930         }
1931
1932         if (fcport->scan_needed) {
1933                 memset(&ea, 0, sizeof(ea));
1934                 ea.id = fcport->d_id;
1935                 ea.id.b.rsvd_1 = RSCN_PORT_ADDR;
1936                 qla2x00_handle_rscn(fcport->vha, &ea);
1937         }
1938 }
1939
1940 static void
1941 qla2x00_tmf_iocb_timeout(void *data)
1942 {
1943         srb_t *sp = data;
1944         struct srb_iocb *tmf = &sp->u.iocb_cmd;
1945         int rc, h;
1946         unsigned long flags;
1947
1948         rc = qla24xx_async_abort_cmd(sp, false);
1949         if (rc) {
1950                 spin_lock_irqsave(sp->qpair->qp_lock_ptr, flags);
1951                 for (h = 1; h < sp->qpair->req->num_outstanding_cmds; h++) {
1952                         if (sp->qpair->req->outstanding_cmds[h] == sp) {
1953                                 sp->qpair->req->outstanding_cmds[h] = NULL;
1954                                 break;
1955                         }
1956                 }
1957                 spin_unlock_irqrestore(sp->qpair->qp_lock_ptr, flags);
1958                 tmf->u.tmf.comp_status = cpu_to_le16(CS_TIMEOUT);
1959                 tmf->u.tmf.data = QLA_FUNCTION_FAILED;
1960                 complete(&tmf->u.tmf.comp);
1961         }
1962 }
1963
1964 static void qla2x00_tmf_sp_done(srb_t *sp, int res)
1965 {
1966         struct srb_iocb *tmf = &sp->u.iocb_cmd;
1967
1968         complete(&tmf->u.tmf.comp);
1969 }
1970
1971 int
1972 qla2x00_async_tm_cmd(fc_port_t *fcport, uint32_t flags, uint32_t lun,
1973         uint32_t tag)
1974 {
1975         struct scsi_qla_host *vha = fcport->vha;
1976         struct srb_iocb *tm_iocb;
1977         srb_t *sp;
1978         int rval = QLA_FUNCTION_FAILED;
1979
1980         /* ref: INIT */
1981         sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
1982         if (!sp)
1983                 goto done;
1984
1985         sp->type = SRB_TM_CMD;
1986         sp->name = "tmf";
1987         qla2x00_init_async_sp(sp, qla2x00_get_async_timeout(vha),
1988                               qla2x00_tmf_sp_done);
1989         sp->u.iocb_cmd.timeout = qla2x00_tmf_iocb_timeout;
1990
1991         tm_iocb = &sp->u.iocb_cmd;
1992         init_completion(&tm_iocb->u.tmf.comp);
1993         tm_iocb->u.tmf.flags = flags;
1994         tm_iocb->u.tmf.lun = lun;
1995
1996         ql_dbg(ql_dbg_taskm, vha, 0x802f,
1997             "Async-tmf hdl=%x loop-id=%x portid=%02x%02x%02x.\n",
1998             sp->handle, fcport->loop_id, fcport->d_id.b.domain,
1999             fcport->d_id.b.area, fcport->d_id.b.al_pa);
2000
2001         rval = qla2x00_start_sp(sp);
2002         if (rval != QLA_SUCCESS)
2003                 goto done_free_sp;
2004         wait_for_completion(&tm_iocb->u.tmf.comp);
2005
2006         rval = tm_iocb->u.tmf.data;
2007
2008         if (rval != QLA_SUCCESS) {
2009                 ql_log(ql_log_warn, vha, 0x8030,
2010                     "TM IOCB failed (%x).\n", rval);
2011         }
2012
2013         if (!test_bit(UNLOADING, &vha->dpc_flags) && !IS_QLAFX00(vha->hw)) {
2014                 flags = tm_iocb->u.tmf.flags;
2015                 lun = (uint16_t)tm_iocb->u.tmf.lun;
2016
2017                 /* Issue Marker IOCB */
2018                 qla2x00_marker(vha, vha->hw->base_qpair,
2019                     fcport->loop_id, lun,
2020                     flags == TCF_LUN_RESET ? MK_SYNC_ID_LUN : MK_SYNC_ID);
2021         }
2022
2023 done_free_sp:
2024         /* ref: INIT */
2025         kref_put(&sp->cmd_kref, qla2x00_sp_release);
2026         fcport->flags &= ~FCF_ASYNC_SENT;
2027 done:
2028         return rval;
2029 }
2030
2031 int
2032 qla24xx_async_abort_command(srb_t *sp)
2033 {
2034         unsigned long   flags = 0;
2035
2036         uint32_t        handle;
2037         fc_port_t       *fcport = sp->fcport;
2038         struct qla_qpair *qpair = sp->qpair;
2039         struct scsi_qla_host *vha = fcport->vha;
2040         struct req_que *req = qpair->req;
2041
2042         spin_lock_irqsave(qpair->qp_lock_ptr, flags);
2043         for (handle = 1; handle < req->num_outstanding_cmds; handle++) {
2044                 if (req->outstanding_cmds[handle] == sp)
2045                         break;
2046         }
2047         spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
2048
2049         if (handle == req->num_outstanding_cmds) {
2050                 /* Command not found. */
2051                 return QLA_ERR_NOT_FOUND;
2052         }
2053         if (sp->type == SRB_FXIOCB_DCMD)
2054                 return qlafx00_fx_disc(vha, &vha->hw->mr.fcport,
2055                     FXDISC_ABORT_IOCTL);
2056
2057         return qla24xx_async_abort_cmd(sp, true);
2058 }
2059
2060 static void
2061 qla24xx_handle_prli_done_event(struct scsi_qla_host *vha, struct event_arg *ea)
2062 {
2063         struct srb *sp;
2064         WARN_ONCE(!qla2xxx_is_valid_mbs(ea->data[0]), "mbs: %#x\n",
2065                   ea->data[0]);
2066
2067         switch (ea->data[0]) {
2068         case MBS_COMMAND_COMPLETE:
2069                 ql_dbg(ql_dbg_disc, vha, 0x2118,
2070                     "%s %d %8phC post gpdb\n",
2071                     __func__, __LINE__, ea->fcport->port_name);
2072
2073                 ea->fcport->chip_reset = vha->hw->base_qpair->chip_reset;
2074                 ea->fcport->logout_on_delete = 1;
2075                 ea->fcport->nvme_prli_service_param = ea->iop[0];
2076                 if (ea->iop[0] & NVME_PRLI_SP_FIRST_BURST)
2077                         ea->fcport->nvme_first_burst_size =
2078                             (ea->iop[1] & 0xffff) * 512;
2079                 else
2080                         ea->fcport->nvme_first_burst_size = 0;
2081                 qla24xx_post_gpdb_work(vha, ea->fcport, 0);
2082                 break;
2083         default:
2084                 if ((ea->iop[0] == LSC_SCODE_ELS_REJECT) &&
2085                     (ea->iop[1] == 0x50000)) {   /* reson 5=busy expl:0x0 */
2086                         set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
2087                         ea->fcport->fw_login_state = DSC_LS_PLOGI_COMP;
2088                         break;
2089                 }
2090
2091                 sp = ea->sp;
2092                 ql_dbg(ql_dbg_disc, vha, 0x2118,
2093                        "%s %d %8phC priority %s, fc4type %x prev try %s\n",
2094                        __func__, __LINE__, ea->fcport->port_name,
2095                        vha->hw->fc4_type_priority == FC4_PRIORITY_FCP ?
2096                        "FCP" : "NVMe", ea->fcport->fc4_type,
2097                        (sp->u.iocb_cmd.u.logio.flags & SRB_LOGIN_NVME_PRLI) ?
2098                         "NVME" : "FCP");
2099
2100                 if (NVME_FCP_TARGET(ea->fcport)) {
2101                         if (sp->u.iocb_cmd.u.logio.flags & SRB_LOGIN_NVME_PRLI)
2102                                 ea->fcport->do_prli_nvme = 0;
2103                         else
2104                                 ea->fcport->do_prli_nvme = 1;
2105                 } else {
2106                         ea->fcport->do_prli_nvme = 0;
2107                 }
2108
2109                 if (N2N_TOPO(vha->hw)) {
2110                         if (ea->fcport->n2n_link_reset_cnt <
2111                             vha->hw->login_retry_count) {
2112                                 ea->fcport->n2n_link_reset_cnt++;
2113                                 vha->relogin_jif = jiffies + 2 * HZ;
2114                                 /*
2115                                  * PRLI failed. Reset link to kick start
2116                                  * state machine
2117                                  */
2118                                 set_bit(N2N_LINK_RESET, &vha->dpc_flags);
2119                                 qla2xxx_wake_dpc(vha);
2120                         } else {
2121                                 ql_log(ql_log_warn, vha, 0x2119,
2122                                        "%s %d %8phC Unable to reconnect\n",
2123                                        __func__, __LINE__,
2124                                        ea->fcport->port_name);
2125                         }
2126                 } else {
2127                         /*
2128                          * switch connect. login failed. Take connection down
2129                          * and allow relogin to retrigger
2130                          */
2131                         ea->fcport->flags &= ~FCF_ASYNC_SENT;
2132                         ea->fcport->keep_nport_handle = 0;
2133                         ea->fcport->logout_on_delete = 1;
2134                         qlt_schedule_sess_for_deletion(ea->fcport);
2135                 }
2136                 break;
2137         }
2138 }
2139
2140 void
2141 qla24xx_handle_plogi_done_event(struct scsi_qla_host *vha, struct event_arg *ea)
2142 {
2143         port_id_t cid;  /* conflict Nport id */
2144         u16 lid;
2145         struct fc_port *conflict_fcport;
2146         unsigned long flags;
2147         struct fc_port *fcport = ea->fcport;
2148
2149         ql_dbg(ql_dbg_disc, vha, 0xffff,
2150             "%s %8phC DS %d LS %d rc %d login %d|%d rscn %d|%d data %x|%x iop %x|%x\n",
2151             __func__, fcport->port_name, fcport->disc_state,
2152             fcport->fw_login_state, ea->rc, ea->sp->gen2, fcport->login_gen,
2153             ea->sp->gen1, fcport->rscn_gen,
2154             ea->data[0], ea->data[1], ea->iop[0], ea->iop[1]);
2155
2156         if ((fcport->fw_login_state == DSC_LS_PLOGI_PEND) ||
2157             (fcport->fw_login_state == DSC_LS_PRLI_PEND)) {
2158                 ql_dbg(ql_dbg_disc, vha, 0x20ea,
2159                     "%s %d %8phC Remote is trying to login\n",
2160                     __func__, __LINE__, fcport->port_name);
2161                 return;
2162         }
2163
2164         if ((fcport->disc_state == DSC_DELETE_PEND) ||
2165             (fcport->disc_state == DSC_DELETED)) {
2166                 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
2167                 return;
2168         }
2169
2170         if (ea->sp->gen2 != fcport->login_gen) {
2171                 /* target side must have changed it. */
2172                 ql_dbg(ql_dbg_disc, vha, 0x20d3,
2173                     "%s %8phC generation changed\n",
2174                     __func__, fcport->port_name);
2175                 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
2176                 return;
2177         } else if (ea->sp->gen1 != fcport->rscn_gen) {
2178                 ql_dbg(ql_dbg_disc, vha, 0x20d3,
2179                     "%s %8phC RSCN generation changed\n",
2180                     __func__, fcport->port_name);
2181                 qla_rscn_replay(fcport);
2182                 qlt_schedule_sess_for_deletion(fcport);
2183                 return;
2184         }
2185
2186         WARN_ONCE(!qla2xxx_is_valid_mbs(ea->data[0]), "mbs: %#x\n",
2187                   ea->data[0]);
2188
2189         switch (ea->data[0]) {
2190         case MBS_COMMAND_COMPLETE:
2191                 /*
2192                  * Driver must validate login state - If PRLI not complete,
2193                  * force a relogin attempt via implicit LOGO, PLOGI, and PRLI
2194                  * requests.
2195                  */
2196                 if (vha->hw->flags.edif_enabled) {
2197                         set_bit(ea->fcport->loop_id, vha->hw->loop_id_map);
2198                         spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
2199                         ea->fcport->chip_reset = vha->hw->base_qpair->chip_reset;
2200                         ea->fcport->logout_on_delete = 1;
2201                         ea->fcport->send_els_logo = 0;
2202                         ea->fcport->fw_login_state = DSC_LS_PLOGI_COMP;
2203                         spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
2204
2205                         qla24xx_post_gpdb_work(vha, ea->fcport, 0);
2206                 } else {
2207                         if (NVME_TARGET(vha->hw, fcport)) {
2208                                 ql_dbg(ql_dbg_disc, vha, 0x2117,
2209                                     "%s %d %8phC post prli\n",
2210                                     __func__, __LINE__, fcport->port_name);
2211                                 qla24xx_post_prli_work(vha, fcport);
2212                         } else {
2213                                 ql_dbg(ql_dbg_disc, vha, 0x20ea,
2214                                     "%s %d %8phC LoopID 0x%x in use with %06x. post gpdb\n",
2215                                     __func__, __LINE__, fcport->port_name,
2216                                     fcport->loop_id, fcport->d_id.b24);
2217
2218                                 set_bit(fcport->loop_id, vha->hw->loop_id_map);
2219                                 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
2220                                 fcport->chip_reset = vha->hw->base_qpair->chip_reset;
2221                                 fcport->logout_on_delete = 1;
2222                                 fcport->send_els_logo = 0;
2223                                 fcport->fw_login_state = DSC_LS_PRLI_COMP;
2224                                 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
2225
2226                                 qla24xx_post_gpdb_work(vha, fcport, 0);
2227                         }
2228                 }
2229                 break;
2230         case MBS_COMMAND_ERROR:
2231                 ql_dbg(ql_dbg_disc, vha, 0x20eb, "%s %d %8phC cmd error %x\n",
2232                     __func__, __LINE__, ea->fcport->port_name, ea->data[1]);
2233
2234                 qlt_schedule_sess_for_deletion(ea->fcport);
2235                 break;
2236         case MBS_LOOP_ID_USED:
2237                 /* data[1] = IO PARAM 1 = nport ID  */
2238                 cid.b.domain = (ea->iop[1] >> 16) & 0xff;
2239                 cid.b.area   = (ea->iop[1] >>  8) & 0xff;
2240                 cid.b.al_pa  = ea->iop[1] & 0xff;
2241                 cid.b.rsvd_1 = 0;
2242
2243                 ql_dbg(ql_dbg_disc, vha, 0x20ec,
2244                     "%s %d %8phC lid %#x in use with pid %06x post gnl\n",
2245                     __func__, __LINE__, ea->fcport->port_name,
2246                     ea->fcport->loop_id, cid.b24);
2247
2248                 set_bit(ea->fcport->loop_id, vha->hw->loop_id_map);
2249                 ea->fcport->loop_id = FC_NO_LOOP_ID;
2250                 qla24xx_post_gnl_work(vha, ea->fcport);
2251                 break;
2252         case MBS_PORT_ID_USED:
2253                 lid = ea->iop[1] & 0xffff;
2254                 qlt_find_sess_invalidate_other(vha,
2255                     wwn_to_u64(ea->fcport->port_name),
2256                     ea->fcport->d_id, lid, &conflict_fcport);
2257
2258                 if (conflict_fcport) {
2259                         /*
2260                          * Another fcport share the same loop_id/nport id.
2261                          * Conflict fcport needs to finish cleanup before this
2262                          * fcport can proceed to login.
2263                          */
2264                         conflict_fcport->conflict = ea->fcport;
2265                         ea->fcport->login_pause = 1;
2266
2267                         ql_dbg(ql_dbg_disc, vha, 0x20ed,
2268                             "%s %d %8phC NPortId %06x inuse with loopid 0x%x. post gidpn\n",
2269                             __func__, __LINE__, ea->fcport->port_name,
2270                             ea->fcport->d_id.b24, lid);
2271                 } else {
2272                         ql_dbg(ql_dbg_disc, vha, 0x20ed,
2273                             "%s %d %8phC NPortId %06x inuse with loopid 0x%x. sched delete\n",
2274                             __func__, __LINE__, ea->fcport->port_name,
2275                             ea->fcport->d_id.b24, lid);
2276
2277                         qla2x00_clear_loop_id(ea->fcport);
2278                         set_bit(lid, vha->hw->loop_id_map);
2279                         ea->fcport->loop_id = lid;
2280                         ea->fcport->keep_nport_handle = 0;
2281                         ea->fcport->logout_on_delete = 1;
2282                         qlt_schedule_sess_for_deletion(ea->fcport);
2283                 }
2284                 break;
2285         }
2286         return;
2287 }
2288
2289 /****************************************************************************/
2290 /*                QLogic ISP2x00 Hardware Support Functions.                */
2291 /****************************************************************************/
2292
2293 static int
2294 qla83xx_nic_core_fw_load(scsi_qla_host_t *vha)
2295 {
2296         int rval = QLA_SUCCESS;
2297         struct qla_hw_data *ha = vha->hw;
2298         uint32_t idc_major_ver, idc_minor_ver;
2299         uint16_t config[4];
2300
2301         qla83xx_idc_lock(vha, 0);
2302
2303         /* SV: TODO: Assign initialization timeout from
2304          * flash-info / other param
2305          */
2306         ha->fcoe_dev_init_timeout = QLA83XX_IDC_INITIALIZATION_TIMEOUT;
2307         ha->fcoe_reset_timeout = QLA83XX_IDC_RESET_ACK_TIMEOUT;
2308
2309         /* Set our fcoe function presence */
2310         if (__qla83xx_set_drv_presence(vha) != QLA_SUCCESS) {
2311                 ql_dbg(ql_dbg_p3p, vha, 0xb077,
2312                     "Error while setting DRV-Presence.\n");
2313                 rval = QLA_FUNCTION_FAILED;
2314                 goto exit;
2315         }
2316
2317         /* Decide the reset ownership */
2318         qla83xx_reset_ownership(vha);
2319
2320         /*
2321          * On first protocol driver load:
2322          * Init-Owner: Set IDC-Major-Version and Clear IDC-Lock-Recovery
2323          * register.
2324          * Others: Check compatibility with current IDC Major version.
2325          */
2326         qla83xx_rd_reg(vha, QLA83XX_IDC_MAJOR_VERSION, &idc_major_ver);
2327         if (ha->flags.nic_core_reset_owner) {
2328                 /* Set IDC Major version */
2329                 idc_major_ver = QLA83XX_SUPP_IDC_MAJOR_VERSION;
2330                 qla83xx_wr_reg(vha, QLA83XX_IDC_MAJOR_VERSION, idc_major_ver);
2331
2332                 /* Clearing IDC-Lock-Recovery register */
2333                 qla83xx_wr_reg(vha, QLA83XX_IDC_LOCK_RECOVERY, 0);
2334         } else if (idc_major_ver != QLA83XX_SUPP_IDC_MAJOR_VERSION) {
2335                 /*
2336                  * Clear further IDC participation if we are not compatible with
2337                  * the current IDC Major Version.
2338                  */
2339                 ql_log(ql_log_warn, vha, 0xb07d,
2340                     "Failing load, idc_major_ver=%d, expected_major_ver=%d.\n",
2341                     idc_major_ver, QLA83XX_SUPP_IDC_MAJOR_VERSION);
2342                 __qla83xx_clear_drv_presence(vha);
2343                 rval = QLA_FUNCTION_FAILED;
2344                 goto exit;
2345         }
2346         /* Each function sets its supported Minor version. */
2347         qla83xx_rd_reg(vha, QLA83XX_IDC_MINOR_VERSION, &idc_minor_ver);
2348         idc_minor_ver |= (QLA83XX_SUPP_IDC_MINOR_VERSION << (ha->portnum * 2));
2349         qla83xx_wr_reg(vha, QLA83XX_IDC_MINOR_VERSION, idc_minor_ver);
2350
2351         if (ha->flags.nic_core_reset_owner) {
2352                 memset(config, 0, sizeof(config));
2353                 if (!qla81xx_get_port_config(vha, config))
2354                         qla83xx_wr_reg(vha, QLA83XX_IDC_DEV_STATE,
2355                             QLA8XXX_DEV_READY);
2356         }
2357
2358         rval = qla83xx_idc_state_handler(vha);
2359
2360 exit:
2361         qla83xx_idc_unlock(vha, 0);
2362
2363         return rval;
2364 }
2365
2366 /*
2367 * qla2x00_initialize_adapter
2368 *      Initialize board.
2369 *
2370 * Input:
2371 *      ha = adapter block pointer.
2372 *
2373 * Returns:
2374 *      0 = success
2375 */
2376 int
2377 qla2x00_initialize_adapter(scsi_qla_host_t *vha)
2378 {
2379         int     rval;
2380         struct qla_hw_data *ha = vha->hw;
2381         struct req_que *req = ha->req_q_map[0];
2382         struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
2383
2384         memset(&vha->qla_stats, 0, sizeof(vha->qla_stats));
2385         memset(&vha->fc_host_stat, 0, sizeof(vha->fc_host_stat));
2386
2387         /* Clear adapter flags. */
2388         vha->flags.online = 0;
2389         ha->flags.chip_reset_done = 0;
2390         vha->flags.reset_active = 0;
2391         ha->flags.pci_channel_io_perm_failure = 0;
2392         ha->flags.eeh_busy = 0;
2393         vha->qla_stats.jiffies_at_last_reset = get_jiffies_64();
2394         atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
2395         atomic_set(&vha->loop_state, LOOP_DOWN);
2396         vha->device_flags = DFLG_NO_CABLE;
2397         vha->dpc_flags = 0;
2398         vha->flags.management_server_logged_in = 0;
2399         vha->marker_needed = 0;
2400         ha->isp_abort_cnt = 0;
2401         ha->beacon_blink_led = 0;
2402
2403         set_bit(0, ha->req_qid_map);
2404         set_bit(0, ha->rsp_qid_map);
2405
2406         ql_dbg(ql_dbg_init, vha, 0x0040,
2407             "Configuring PCI space...\n");
2408         rval = ha->isp_ops->pci_config(vha);
2409         if (rval) {
2410                 ql_log(ql_log_warn, vha, 0x0044,
2411                     "Unable to configure PCI space.\n");
2412                 return (rval);
2413         }
2414
2415         ha->isp_ops->reset_chip(vha);
2416
2417         /* Check for secure flash support */
2418         if (IS_QLA28XX(ha)) {
2419                 if (rd_reg_word(&reg->mailbox12) & BIT_0)
2420                         ha->flags.secure_adapter = 1;
2421                 ql_log(ql_log_info, vha, 0xffff, "Secure Adapter: %s\n",
2422                     (ha->flags.secure_adapter) ? "Yes" : "No");
2423         }
2424
2425
2426         rval = qla2xxx_get_flash_info(vha);
2427         if (rval) {
2428                 ql_log(ql_log_fatal, vha, 0x004f,
2429                     "Unable to validate FLASH data.\n");
2430                 return rval;
2431         }
2432
2433         if (IS_QLA8044(ha)) {
2434                 qla8044_read_reset_template(vha);
2435
2436                 /* NOTE: If ql2xdontresethba==1, set IDC_CTRL DONTRESET_BIT0.
2437                  * If DONRESET_BIT0 is set, drivers should not set dev_state
2438                  * to NEED_RESET. But if NEED_RESET is set, drivers should
2439                  * should honor the reset. */
2440                 if (ql2xdontresethba == 1)
2441                         qla8044_set_idc_dontreset(vha);
2442         }
2443
2444         ha->isp_ops->get_flash_version(vha, req->ring);
2445         ql_dbg(ql_dbg_init, vha, 0x0061,
2446             "Configure NVRAM parameters...\n");
2447
2448         /* Let priority default to FCP, can be overridden by nvram_config */
2449         ha->fc4_type_priority = FC4_PRIORITY_FCP;
2450
2451         ha->isp_ops->nvram_config(vha);
2452
2453         if (ha->fc4_type_priority != FC4_PRIORITY_FCP &&
2454             ha->fc4_type_priority != FC4_PRIORITY_NVME)
2455                 ha->fc4_type_priority = FC4_PRIORITY_FCP;
2456
2457         ql_log(ql_log_info, vha, 0xffff, "FC4 priority set to %s\n",
2458                ha->fc4_type_priority == FC4_PRIORITY_FCP ? "FCP" : "NVMe");
2459
2460         if (ha->flags.disable_serdes) {
2461                 /* Mask HBA via NVRAM settings? */
2462                 ql_log(ql_log_info, vha, 0x0077,
2463                     "Masking HBA WWPN %8phN (via NVRAM).\n", vha->port_name);
2464                 return QLA_FUNCTION_FAILED;
2465         }
2466
2467         ql_dbg(ql_dbg_init, vha, 0x0078,
2468             "Verifying loaded RISC code...\n");
2469
2470         /* If smartsan enabled then require fdmi and rdp enabled */
2471         if (ql2xsmartsan) {
2472                 ql2xfdmienable = 1;
2473                 ql2xrdpenable = 1;
2474         }
2475
2476         if (qla2x00_isp_firmware(vha) != QLA_SUCCESS) {
2477                 rval = ha->isp_ops->chip_diag(vha);
2478                 if (rval)
2479                         return (rval);
2480                 rval = qla2x00_setup_chip(vha);
2481                 if (rval)
2482                         return (rval);
2483         }
2484
2485         if (IS_QLA84XX(ha)) {
2486                 ha->cs84xx = qla84xx_get_chip(vha);
2487                 if (!ha->cs84xx) {
2488                         ql_log(ql_log_warn, vha, 0x00d0,
2489                             "Unable to configure ISP84XX.\n");
2490                         return QLA_FUNCTION_FAILED;
2491                 }
2492         }
2493
2494         if (qla_ini_mode_enabled(vha) || qla_dual_mode_enabled(vha))
2495                 rval = qla2x00_init_rings(vha);
2496
2497         /* No point in continuing if firmware initialization failed. */
2498         if (rval != QLA_SUCCESS)
2499                 return rval;
2500
2501         ha->flags.chip_reset_done = 1;
2502
2503         if (rval == QLA_SUCCESS && IS_QLA84XX(ha)) {
2504                 /* Issue verify 84xx FW IOCB to complete 84xx initialization */
2505                 rval = qla84xx_init_chip(vha);
2506                 if (rval != QLA_SUCCESS) {
2507                         ql_log(ql_log_warn, vha, 0x00d4,
2508                             "Unable to initialize ISP84XX.\n");
2509                         qla84xx_put_chip(vha);
2510                 }
2511         }
2512
2513         /* Load the NIC Core f/w if we are the first protocol driver. */
2514         if (IS_QLA8031(ha)) {
2515                 rval = qla83xx_nic_core_fw_load(vha);
2516                 if (rval)
2517                         ql_log(ql_log_warn, vha, 0x0124,
2518                             "Error in initializing NIC Core f/w.\n");
2519         }
2520
2521         if (IS_QLA24XX_TYPE(ha) || IS_QLA25XX(ha))
2522                 qla24xx_read_fcp_prio_cfg(vha);
2523
2524         if (IS_P3P_TYPE(ha))
2525                 qla82xx_set_driver_version(vha, QLA2XXX_VERSION);
2526         else
2527                 qla25xx_set_driver_version(vha, QLA2XXX_VERSION);
2528
2529         return (rval);
2530 }
2531
2532 /**
2533  * qla2100_pci_config() - Setup ISP21xx PCI configuration registers.
2534  * @vha: HA context
2535  *
2536  * Returns 0 on success.
2537  */
2538 int
2539 qla2100_pci_config(scsi_qla_host_t *vha)
2540 {
2541         uint16_t w;
2542         unsigned long flags;
2543         struct qla_hw_data *ha = vha->hw;
2544         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
2545
2546         pci_set_master(ha->pdev);
2547         pci_try_set_mwi(ha->pdev);
2548
2549         pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
2550         w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
2551         pci_write_config_word(ha->pdev, PCI_COMMAND, w);
2552
2553         pci_disable_rom(ha->pdev);
2554
2555         /* Get PCI bus information. */
2556         spin_lock_irqsave(&ha->hardware_lock, flags);
2557         ha->pci_attr = rd_reg_word(&reg->ctrl_status);
2558         spin_unlock_irqrestore(&ha->hardware_lock, flags);
2559
2560         return QLA_SUCCESS;
2561 }
2562
2563 /**
2564  * qla2300_pci_config() - Setup ISP23xx PCI configuration registers.
2565  * @vha: HA context
2566  *
2567  * Returns 0 on success.
2568  */
2569 int
2570 qla2300_pci_config(scsi_qla_host_t *vha)
2571 {
2572         uint16_t        w;
2573         unsigned long   flags = 0;
2574         uint32_t        cnt;
2575         struct qla_hw_data *ha = vha->hw;
2576         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
2577
2578         pci_set_master(ha->pdev);
2579         pci_try_set_mwi(ha->pdev);
2580
2581         pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
2582         w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
2583
2584         if (IS_QLA2322(ha) || IS_QLA6322(ha))
2585                 w &= ~PCI_COMMAND_INTX_DISABLE;
2586         pci_write_config_word(ha->pdev, PCI_COMMAND, w);
2587
2588         /*
2589          * If this is a 2300 card and not 2312, reset the
2590          * COMMAND_INVALIDATE due to a bug in the 2300. Unfortunately,
2591          * the 2310 also reports itself as a 2300 so we need to get the
2592          * fb revision level -- a 6 indicates it really is a 2300 and
2593          * not a 2310.
2594          */
2595         if (IS_QLA2300(ha)) {
2596                 spin_lock_irqsave(&ha->hardware_lock, flags);
2597
2598                 /* Pause RISC. */
2599                 wrt_reg_word(&reg->hccr, HCCR_PAUSE_RISC);
2600                 for (cnt = 0; cnt < 30000; cnt++) {
2601                         if ((rd_reg_word(&reg->hccr) & HCCR_RISC_PAUSE) != 0)
2602                                 break;
2603
2604                         udelay(10);
2605                 }
2606
2607                 /* Select FPM registers. */
2608                 wrt_reg_word(&reg->ctrl_status, 0x20);
2609                 rd_reg_word(&reg->ctrl_status);
2610
2611                 /* Get the fb rev level */
2612                 ha->fb_rev = RD_FB_CMD_REG(ha, reg);
2613
2614                 if (ha->fb_rev == FPM_2300)
2615                         pci_clear_mwi(ha->pdev);
2616
2617                 /* Deselect FPM registers. */
2618                 wrt_reg_word(&reg->ctrl_status, 0x0);
2619                 rd_reg_word(&reg->ctrl_status);
2620
2621                 /* Release RISC module. */
2622                 wrt_reg_word(&reg->hccr, HCCR_RELEASE_RISC);
2623                 for (cnt = 0; cnt < 30000; cnt++) {
2624                         if ((rd_reg_word(&reg->hccr) & HCCR_RISC_PAUSE) == 0)
2625                                 break;
2626
2627                         udelay(10);
2628                 }
2629
2630                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2631         }
2632
2633         pci_write_config_byte(ha->pdev, PCI_LATENCY_TIMER, 0x80);
2634
2635         pci_disable_rom(ha->pdev);
2636
2637         /* Get PCI bus information. */
2638         spin_lock_irqsave(&ha->hardware_lock, flags);
2639         ha->pci_attr = rd_reg_word(&reg->ctrl_status);
2640         spin_unlock_irqrestore(&ha->hardware_lock, flags);
2641
2642         return QLA_SUCCESS;
2643 }
2644
2645 /**
2646  * qla24xx_pci_config() - Setup ISP24xx PCI configuration registers.
2647  * @vha: HA context
2648  *
2649  * Returns 0 on success.
2650  */
2651 int
2652 qla24xx_pci_config(scsi_qla_host_t *vha)
2653 {
2654         uint16_t w;
2655         unsigned long flags = 0;
2656         struct qla_hw_data *ha = vha->hw;
2657         struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
2658
2659         pci_set_master(ha->pdev);
2660         pci_try_set_mwi(ha->pdev);
2661
2662         pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
2663         w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
2664         w &= ~PCI_COMMAND_INTX_DISABLE;
2665         pci_write_config_word(ha->pdev, PCI_COMMAND, w);
2666
2667         pci_write_config_byte(ha->pdev, PCI_LATENCY_TIMER, 0x80);
2668
2669         /* PCI-X -- adjust Maximum Memory Read Byte Count (2048). */
2670         if (pci_find_capability(ha->pdev, PCI_CAP_ID_PCIX))
2671                 pcix_set_mmrbc(ha->pdev, 2048);
2672
2673         /* PCIe -- adjust Maximum Read Request Size (2048). */
2674         if (pci_is_pcie(ha->pdev))
2675                 pcie_set_readrq(ha->pdev, 4096);
2676
2677         pci_disable_rom(ha->pdev);
2678
2679         ha->chip_revision = ha->pdev->revision;
2680
2681         /* Get PCI bus information. */
2682         spin_lock_irqsave(&ha->hardware_lock, flags);
2683         ha->pci_attr = rd_reg_dword(&reg->ctrl_status);
2684         spin_unlock_irqrestore(&ha->hardware_lock, flags);
2685
2686         return QLA_SUCCESS;
2687 }
2688
2689 /**
2690  * qla25xx_pci_config() - Setup ISP25xx PCI configuration registers.
2691  * @vha: HA context
2692  *
2693  * Returns 0 on success.
2694  */
2695 int
2696 qla25xx_pci_config(scsi_qla_host_t *vha)
2697 {
2698         uint16_t w;
2699         struct qla_hw_data *ha = vha->hw;
2700
2701         pci_set_master(ha->pdev);
2702         pci_try_set_mwi(ha->pdev);
2703
2704         pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
2705         w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
2706         w &= ~PCI_COMMAND_INTX_DISABLE;
2707         pci_write_config_word(ha->pdev, PCI_COMMAND, w);
2708
2709         /* PCIe -- adjust Maximum Read Request Size (2048). */
2710         if (pci_is_pcie(ha->pdev))
2711                 pcie_set_readrq(ha->pdev, 4096);
2712
2713         pci_disable_rom(ha->pdev);
2714
2715         ha->chip_revision = ha->pdev->revision;
2716
2717         return QLA_SUCCESS;
2718 }
2719
2720 /**
2721  * qla2x00_isp_firmware() - Choose firmware image.
2722  * @vha: HA context
2723  *
2724  * Returns 0 on success.
2725  */
2726 static int
2727 qla2x00_isp_firmware(scsi_qla_host_t *vha)
2728 {
2729         int  rval;
2730         uint16_t loop_id, topo, sw_cap;
2731         uint8_t domain, area, al_pa;
2732         struct qla_hw_data *ha = vha->hw;
2733
2734         /* Assume loading risc code */
2735         rval = QLA_FUNCTION_FAILED;
2736
2737         if (ha->flags.disable_risc_code_load) {
2738                 ql_log(ql_log_info, vha, 0x0079, "RISC CODE NOT loaded.\n");
2739
2740                 /* Verify checksum of loaded RISC code. */
2741                 rval = qla2x00_verify_checksum(vha, ha->fw_srisc_address);
2742                 if (rval == QLA_SUCCESS) {
2743                         /* And, verify we are not in ROM code. */
2744                         rval = qla2x00_get_adapter_id(vha, &loop_id, &al_pa,
2745                             &area, &domain, &topo, &sw_cap);
2746                 }
2747         }
2748
2749         if (rval)
2750                 ql_dbg(ql_dbg_init, vha, 0x007a,
2751                     "**** Load RISC code ****.\n");
2752
2753         return (rval);
2754 }
2755
2756 /**
2757  * qla2x00_reset_chip() - Reset ISP chip.
2758  * @vha: HA context
2759  *
2760  * Returns 0 on success.
2761  */
2762 int
2763 qla2x00_reset_chip(scsi_qla_host_t *vha)
2764 {
2765         unsigned long   flags = 0;
2766         struct qla_hw_data *ha = vha->hw;
2767         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
2768         uint32_t        cnt;
2769         uint16_t        cmd;
2770         int rval = QLA_FUNCTION_FAILED;
2771
2772         if (unlikely(pci_channel_offline(ha->pdev)))
2773                 return rval;
2774
2775         ha->isp_ops->disable_intrs(ha);
2776
2777         spin_lock_irqsave(&ha->hardware_lock, flags);
2778
2779         /* Turn off master enable */
2780         cmd = 0;
2781         pci_read_config_word(ha->pdev, PCI_COMMAND, &cmd);
2782         cmd &= ~PCI_COMMAND_MASTER;
2783         pci_write_config_word(ha->pdev, PCI_COMMAND, cmd);
2784
2785         if (!IS_QLA2100(ha)) {
2786                 /* Pause RISC. */
2787                 wrt_reg_word(&reg->hccr, HCCR_PAUSE_RISC);
2788                 if (IS_QLA2200(ha) || IS_QLA2300(ha)) {
2789                         for (cnt = 0; cnt < 30000; cnt++) {
2790                                 if ((rd_reg_word(&reg->hccr) &
2791                                     HCCR_RISC_PAUSE) != 0)
2792                                         break;
2793                                 udelay(100);
2794                         }
2795                 } else {
2796                         rd_reg_word(&reg->hccr);        /* PCI Posting. */
2797                         udelay(10);
2798                 }
2799
2800                 /* Select FPM registers. */
2801                 wrt_reg_word(&reg->ctrl_status, 0x20);
2802                 rd_reg_word(&reg->ctrl_status);         /* PCI Posting. */
2803
2804                 /* FPM Soft Reset. */
2805                 wrt_reg_word(&reg->fpm_diag_config, 0x100);
2806                 rd_reg_word(&reg->fpm_diag_config);     /* PCI Posting. */
2807
2808                 /* Toggle Fpm Reset. */
2809                 if (!IS_QLA2200(ha)) {
2810                         wrt_reg_word(&reg->fpm_diag_config, 0x0);
2811                         rd_reg_word(&reg->fpm_diag_config); /* PCI Posting. */
2812                 }
2813
2814                 /* Select frame buffer registers. */
2815                 wrt_reg_word(&reg->ctrl_status, 0x10);
2816                 rd_reg_word(&reg->ctrl_status);         /* PCI Posting. */
2817
2818                 /* Reset frame buffer FIFOs. */
2819                 if (IS_QLA2200(ha)) {
2820                         WRT_FB_CMD_REG(ha, reg, 0xa000);
2821                         RD_FB_CMD_REG(ha, reg);         /* PCI Posting. */
2822                 } else {
2823                         WRT_FB_CMD_REG(ha, reg, 0x00fc);
2824
2825                         /* Read back fb_cmd until zero or 3 seconds max */
2826                         for (cnt = 0; cnt < 3000; cnt++) {
2827                                 if ((RD_FB_CMD_REG(ha, reg) & 0xff) == 0)
2828                                         break;
2829                                 udelay(100);
2830                         }
2831                 }
2832
2833                 /* Select RISC module registers. */
2834                 wrt_reg_word(&reg->ctrl_status, 0);
2835                 rd_reg_word(&reg->ctrl_status);         /* PCI Posting. */
2836
2837                 /* Reset RISC processor. */
2838                 wrt_reg_word(&reg->hccr, HCCR_RESET_RISC);
2839                 rd_reg_word(&reg->hccr);                /* PCI Posting. */
2840
2841                 /* Release RISC processor. */
2842                 wrt_reg_word(&reg->hccr, HCCR_RELEASE_RISC);
2843                 rd_reg_word(&reg->hccr);                /* PCI Posting. */
2844         }
2845
2846         wrt_reg_word(&reg->hccr, HCCR_CLR_RISC_INT);
2847         wrt_reg_word(&reg->hccr, HCCR_CLR_HOST_INT);
2848
2849         /* Reset ISP chip. */
2850         wrt_reg_word(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
2851
2852         /* Wait for RISC to recover from reset. */
2853         if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
2854                 /*
2855                  * It is necessary to for a delay here since the card doesn't
2856                  * respond to PCI reads during a reset. On some architectures
2857                  * this will result in an MCA.
2858                  */
2859                 udelay(20);
2860                 for (cnt = 30000; cnt; cnt--) {
2861                         if ((rd_reg_word(&reg->ctrl_status) &
2862                             CSR_ISP_SOFT_RESET) == 0)
2863                                 break;
2864                         udelay(100);
2865                 }
2866         } else
2867                 udelay(10);
2868
2869         /* Reset RISC processor. */
2870         wrt_reg_word(&reg->hccr, HCCR_RESET_RISC);
2871
2872         wrt_reg_word(&reg->semaphore, 0);
2873
2874         /* Release RISC processor. */
2875         wrt_reg_word(&reg->hccr, HCCR_RELEASE_RISC);
2876         rd_reg_word(&reg->hccr);                        /* PCI Posting. */
2877
2878         if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
2879                 for (cnt = 0; cnt < 30000; cnt++) {
2880                         if (RD_MAILBOX_REG(ha, reg, 0) != MBS_BUSY)
2881                                 break;
2882
2883                         udelay(100);
2884                 }
2885         } else
2886                 udelay(100);
2887
2888         /* Turn on master enable */
2889         cmd |= PCI_COMMAND_MASTER;
2890         pci_write_config_word(ha->pdev, PCI_COMMAND, cmd);
2891
2892         /* Disable RISC pause on FPM parity error. */
2893         if (!IS_QLA2100(ha)) {
2894                 wrt_reg_word(&reg->hccr, HCCR_DISABLE_PARITY_PAUSE);
2895                 rd_reg_word(&reg->hccr);                /* PCI Posting. */
2896         }
2897
2898         spin_unlock_irqrestore(&ha->hardware_lock, flags);
2899
2900         return QLA_SUCCESS;
2901 }
2902
2903 /**
2904  * qla81xx_reset_mpi() - Reset's MPI FW via Write MPI Register MBC.
2905  * @vha: HA context
2906  *
2907  * Returns 0 on success.
2908  */
2909 static int
2910 qla81xx_reset_mpi(scsi_qla_host_t *vha)
2911 {
2912         uint16_t mb[4] = {0x1010, 0, 1, 0};
2913
2914         if (!IS_QLA81XX(vha->hw))
2915                 return QLA_SUCCESS;
2916
2917         return qla81xx_write_mpi_register(vha, mb);
2918 }
2919
2920 static int
2921 qla_chk_risc_recovery(scsi_qla_host_t *vha)
2922 {
2923         struct qla_hw_data *ha = vha->hw;
2924         struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
2925         __le16 __iomem *mbptr = &reg->mailbox0;
2926         int i;
2927         u16 mb[32];
2928         int rc = QLA_SUCCESS;
2929
2930         if (!IS_QLA27XX(ha) && !IS_QLA28XX(ha))
2931                 return rc;
2932
2933         /* this check is only valid after RISC reset */
2934         mb[0] = rd_reg_word(mbptr);
2935         mbptr++;
2936         if (mb[0] == 0xf) {
2937                 rc = QLA_FUNCTION_FAILED;
2938
2939                 for (i = 1; i < 32; i++) {
2940                         mb[i] = rd_reg_word(mbptr);
2941                         mbptr++;
2942                 }
2943
2944                 ql_log(ql_log_warn, vha, 0x1015,
2945                        "RISC reset failed. mb[0-7] %04xh %04xh %04xh %04xh %04xh %04xh %04xh %04xh\n",
2946                        mb[0], mb[1], mb[2], mb[3], mb[4], mb[5], mb[6], mb[7]);
2947                 ql_log(ql_log_warn, vha, 0x1015,
2948                        "RISC reset failed. mb[8-15] %04xh %04xh %04xh %04xh %04xh %04xh %04xh %04xh\n",
2949                        mb[8], mb[9], mb[10], mb[11], mb[12], mb[13], mb[14],
2950                        mb[15]);
2951                 ql_log(ql_log_warn, vha, 0x1015,
2952                        "RISC reset failed. mb[16-23] %04xh %04xh %04xh %04xh %04xh %04xh %04xh %04xh\n",
2953                        mb[16], mb[17], mb[18], mb[19], mb[20], mb[21], mb[22],
2954                        mb[23]);
2955                 ql_log(ql_log_warn, vha, 0x1015,
2956                        "RISC reset failed. mb[24-31] %04xh %04xh %04xh %04xh %04xh %04xh %04xh %04xh\n",
2957                        mb[24], mb[25], mb[26], mb[27], mb[28], mb[29], mb[30],
2958                        mb[31]);
2959         }
2960         return rc;
2961 }
2962
2963 /**
2964  * qla24xx_reset_risc() - Perform full reset of ISP24xx RISC.
2965  * @vha: HA context
2966  *
2967  * Returns 0 on success.
2968  */
2969 static inline int
2970 qla24xx_reset_risc(scsi_qla_host_t *vha)
2971 {
2972         unsigned long flags = 0;
2973         struct qla_hw_data *ha = vha->hw;
2974         struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
2975         uint32_t cnt;
2976         uint16_t wd;
2977         static int abts_cnt; /* ISP abort retry counts */
2978         int rval = QLA_SUCCESS;
2979         int print = 1;
2980
2981         spin_lock_irqsave(&ha->hardware_lock, flags);
2982
2983         /* Reset RISC. */
2984         wrt_reg_dword(&reg->ctrl_status, CSRX_DMA_SHUTDOWN|MWB_4096_BYTES);
2985         for (cnt = 0; cnt < 30000; cnt++) {
2986                 if ((rd_reg_dword(&reg->ctrl_status) & CSRX_DMA_ACTIVE) == 0)
2987                         break;
2988
2989                 udelay(10);
2990         }
2991
2992         if (!(rd_reg_dword(&reg->ctrl_status) & CSRX_DMA_ACTIVE))
2993                 set_bit(DMA_SHUTDOWN_CMPL, &ha->fw_dump_cap_flags);
2994
2995         ql_dbg(ql_dbg_init + ql_dbg_verbose, vha, 0x017e,
2996             "HCCR: 0x%x, Control Status %x, DMA active status:0x%x\n",
2997             rd_reg_dword(&reg->hccr),
2998             rd_reg_dword(&reg->ctrl_status),
2999             (rd_reg_dword(&reg->ctrl_status) & CSRX_DMA_ACTIVE));
3000
3001         wrt_reg_dword(&reg->ctrl_status,
3002             CSRX_ISP_SOFT_RESET|CSRX_DMA_SHUTDOWN|MWB_4096_BYTES);
3003         pci_read_config_word(ha->pdev, PCI_COMMAND, &wd);
3004
3005         udelay(100);
3006
3007         /* Wait for firmware to complete NVRAM accesses. */
3008         rd_reg_word(&reg->mailbox0);
3009         for (cnt = 10000; rd_reg_word(&reg->mailbox0) != 0 &&
3010             rval == QLA_SUCCESS; cnt--) {
3011                 barrier();
3012                 if (cnt)
3013                         udelay(5);
3014                 else
3015                         rval = QLA_FUNCTION_TIMEOUT;
3016         }
3017
3018         if (rval == QLA_SUCCESS)
3019                 set_bit(ISP_MBX_RDY, &ha->fw_dump_cap_flags);
3020
3021         ql_dbg(ql_dbg_init + ql_dbg_verbose, vha, 0x017f,
3022             "HCCR: 0x%x, MailBox0 Status 0x%x\n",
3023             rd_reg_dword(&reg->hccr),
3024             rd_reg_word(&reg->mailbox0));
3025
3026         /* Wait for soft-reset to complete. */
3027         rd_reg_dword(&reg->ctrl_status);
3028         for (cnt = 0; cnt < 60; cnt++) {
3029                 barrier();
3030                 if ((rd_reg_dword(&reg->ctrl_status) &
3031                     CSRX_ISP_SOFT_RESET) == 0)
3032                         break;
3033
3034                 udelay(5);
3035         }
3036         if (!(rd_reg_dword(&reg->ctrl_status) & CSRX_ISP_SOFT_RESET))
3037                 set_bit(ISP_SOFT_RESET_CMPL, &ha->fw_dump_cap_flags);
3038
3039         ql_dbg(ql_dbg_init + ql_dbg_verbose, vha, 0x015d,
3040             "HCCR: 0x%x, Soft Reset status: 0x%x\n",
3041             rd_reg_dword(&reg->hccr),
3042             rd_reg_dword(&reg->ctrl_status));
3043
3044         /* If required, do an MPI FW reset now */
3045         if (test_and_clear_bit(MPI_RESET_NEEDED, &vha->dpc_flags)) {
3046                 if (qla81xx_reset_mpi(vha) != QLA_SUCCESS) {
3047                         if (++abts_cnt < 5) {
3048                                 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
3049                                 set_bit(MPI_RESET_NEEDED, &vha->dpc_flags);
3050                         } else {
3051                                 /*
3052                                  * We exhausted the ISP abort retries. We have to
3053                                  * set the board offline.
3054                                  */
3055                                 abts_cnt = 0;
3056                                 vha->flags.online = 0;
3057                         }
3058                 }
3059         }
3060
3061         wrt_reg_dword(&reg->hccr, HCCRX_SET_RISC_RESET);
3062         rd_reg_dword(&reg->hccr);
3063
3064         wrt_reg_dword(&reg->hccr, HCCRX_REL_RISC_PAUSE);
3065         rd_reg_dword(&reg->hccr);
3066
3067         wrt_reg_dword(&reg->hccr, HCCRX_CLR_RISC_RESET);
3068         mdelay(10);
3069         rd_reg_dword(&reg->hccr);
3070
3071         wd = rd_reg_word(&reg->mailbox0);
3072         for (cnt = 300; wd != 0 && rval == QLA_SUCCESS; cnt--) {
3073                 barrier();
3074                 if (cnt) {
3075                         mdelay(1);
3076                         if (print && qla_chk_risc_recovery(vha))
3077                                 print = 0;
3078
3079                         wd = rd_reg_word(&reg->mailbox0);
3080                 } else {
3081                         rval = QLA_FUNCTION_TIMEOUT;
3082
3083                         ql_log(ql_log_warn, vha, 0x015e,
3084                                "RISC reset timeout\n");
3085                 }
3086         }
3087
3088         if (rval == QLA_SUCCESS)
3089                 set_bit(RISC_RDY_AFT_RESET, &ha->fw_dump_cap_flags);
3090
3091         ql_dbg(ql_dbg_init + ql_dbg_verbose, vha, 0x015e,
3092             "Host Risc 0x%x, mailbox0 0x%x\n",
3093             rd_reg_dword(&reg->hccr),
3094              rd_reg_word(&reg->mailbox0));
3095
3096         spin_unlock_irqrestore(&ha->hardware_lock, flags);
3097
3098         ql_dbg(ql_dbg_init + ql_dbg_verbose, vha, 0x015f,
3099             "Driver in %s mode\n",
3100             IS_NOPOLLING_TYPE(ha) ? "Interrupt" : "Polling");
3101
3102         if (IS_NOPOLLING_TYPE(ha))
3103                 ha->isp_ops->enable_intrs(ha);
3104
3105         return rval;
3106 }
3107
3108 static void
3109 qla25xx_read_risc_sema_reg(scsi_qla_host_t *vha, uint32_t *data)
3110 {
3111         struct device_reg_24xx __iomem *reg = &vha->hw->iobase->isp24;
3112
3113         wrt_reg_dword(&reg->iobase_addr, RISC_REGISTER_BASE_OFFSET);
3114         *data = rd_reg_dword(&reg->iobase_window + RISC_REGISTER_WINDOW_OFFSET);
3115 }
3116
3117 static void
3118 qla25xx_write_risc_sema_reg(scsi_qla_host_t *vha, uint32_t data)
3119 {
3120         struct device_reg_24xx __iomem *reg = &vha->hw->iobase->isp24;
3121
3122         wrt_reg_dword(&reg->iobase_addr, RISC_REGISTER_BASE_OFFSET);
3123         wrt_reg_dword(&reg->iobase_window + RISC_REGISTER_WINDOW_OFFSET, data);
3124 }
3125
3126 static void
3127 qla25xx_manipulate_risc_semaphore(scsi_qla_host_t *vha)
3128 {
3129         uint32_t wd32 = 0;
3130         uint delta_msec = 100;
3131         uint elapsed_msec = 0;
3132         uint timeout_msec;
3133         ulong n;
3134
3135         if (vha->hw->pdev->subsystem_device != 0x0175 &&
3136             vha->hw->pdev->subsystem_device != 0x0240)
3137                 return;
3138
3139         wrt_reg_dword(&vha->hw->iobase->isp24.hccr, HCCRX_SET_RISC_PAUSE);
3140         udelay(100);
3141
3142 attempt:
3143         timeout_msec = TIMEOUT_SEMAPHORE;
3144         n = timeout_msec / delta_msec;
3145         while (n--) {
3146                 qla25xx_write_risc_sema_reg(vha, RISC_SEMAPHORE_SET);
3147                 qla25xx_read_risc_sema_reg(vha, &wd32);
3148                 if (wd32 & RISC_SEMAPHORE)
3149                         break;
3150                 msleep(delta_msec);
3151                 elapsed_msec += delta_msec;
3152                 if (elapsed_msec > TIMEOUT_TOTAL_ELAPSED)
3153                         goto force;
3154         }
3155
3156         if (!(wd32 & RISC_SEMAPHORE))
3157                 goto force;
3158
3159         if (!(wd32 & RISC_SEMAPHORE_FORCE))
3160                 goto acquired;
3161
3162         qla25xx_write_risc_sema_reg(vha, RISC_SEMAPHORE_CLR);
3163         timeout_msec = TIMEOUT_SEMAPHORE_FORCE;
3164         n = timeout_msec / delta_msec;
3165         while (n--) {
3166                 qla25xx_read_risc_sema_reg(vha, &wd32);
3167                 if (!(wd32 & RISC_SEMAPHORE_FORCE))
3168                         break;
3169                 msleep(delta_msec);
3170                 elapsed_msec += delta_msec;
3171                 if (elapsed_msec > TIMEOUT_TOTAL_ELAPSED)
3172                         goto force;
3173         }
3174
3175         if (wd32 & RISC_SEMAPHORE_FORCE)
3176                 qla25xx_write_risc_sema_reg(vha, RISC_SEMAPHORE_FORCE_CLR);
3177
3178         goto attempt;
3179
3180 force:
3181         qla25xx_write_risc_sema_reg(vha, RISC_SEMAPHORE_FORCE_SET);
3182
3183 acquired:
3184         return;
3185 }
3186
3187 /**
3188  * qla24xx_reset_chip() - Reset ISP24xx chip.
3189  * @vha: HA context
3190  *
3191  * Returns 0 on success.
3192  */
3193 int
3194 qla24xx_reset_chip(scsi_qla_host_t *vha)
3195 {
3196         struct qla_hw_data *ha = vha->hw;
3197         int rval = QLA_FUNCTION_FAILED;
3198
3199         if (pci_channel_offline(ha->pdev) &&
3200             ha->flags.pci_channel_io_perm_failure) {
3201                 return rval;
3202         }
3203
3204         ha->isp_ops->disable_intrs(ha);
3205
3206         qla25xx_manipulate_risc_semaphore(vha);
3207
3208         /* Perform RISC reset. */
3209         rval = qla24xx_reset_risc(vha);
3210
3211         return rval;
3212 }
3213
3214 /**
3215  * qla2x00_chip_diag() - Test chip for proper operation.
3216  * @vha: HA context
3217  *
3218  * Returns 0 on success.
3219  */
3220 int
3221 qla2x00_chip_diag(scsi_qla_host_t *vha)
3222 {
3223         int             rval;
3224         struct qla_hw_data *ha = vha->hw;
3225         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
3226         unsigned long   flags = 0;
3227         uint16_t        data;
3228         uint32_t        cnt;
3229         uint16_t        mb[5];
3230         struct req_que *req = ha->req_q_map[0];
3231
3232         /* Assume a failed state */
3233         rval = QLA_FUNCTION_FAILED;
3234
3235         ql_dbg(ql_dbg_init, vha, 0x007b, "Testing device at %p.\n",
3236                &reg->flash_address);
3237
3238         spin_lock_irqsave(&ha->hardware_lock, flags);
3239
3240         /* Reset ISP chip. */
3241         wrt_reg_word(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
3242
3243         /*
3244          * We need to have a delay here since the card will not respond while
3245          * in reset causing an MCA on some architectures.
3246          */
3247         udelay(20);
3248         data = qla2x00_debounce_register(&reg->ctrl_status);
3249         for (cnt = 6000000 ; cnt && (data & CSR_ISP_SOFT_RESET); cnt--) {
3250                 udelay(5);
3251                 data = rd_reg_word(&reg->ctrl_status);
3252                 barrier();
3253         }
3254
3255         if (!cnt)
3256                 goto chip_diag_failed;
3257
3258         ql_dbg(ql_dbg_init, vha, 0x007c,
3259             "Reset register cleared by chip reset.\n");
3260
3261         /* Reset RISC processor. */
3262         wrt_reg_word(&reg->hccr, HCCR_RESET_RISC);
3263         wrt_reg_word(&reg->hccr, HCCR_RELEASE_RISC);
3264
3265         /* Workaround for QLA2312 PCI parity error */
3266         if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
3267                 data = qla2x00_debounce_register(MAILBOX_REG(ha, reg, 0));
3268                 for (cnt = 6000000; cnt && (data == MBS_BUSY); cnt--) {
3269                         udelay(5);
3270                         data = RD_MAILBOX_REG(ha, reg, 0);
3271                         barrier();
3272                 }
3273         } else
3274                 udelay(10);
3275
3276         if (!cnt)
3277                 goto chip_diag_failed;
3278
3279         /* Check product ID of chip */
3280         ql_dbg(ql_dbg_init, vha, 0x007d, "Checking product ID of chip.\n");
3281
3282         mb[1] = RD_MAILBOX_REG(ha, reg, 1);
3283         mb[2] = RD_MAILBOX_REG(ha, reg, 2);
3284         mb[3] = RD_MAILBOX_REG(ha, reg, 3);
3285         mb[4] = qla2x00_debounce_register(MAILBOX_REG(ha, reg, 4));
3286         if (mb[1] != PROD_ID_1 || (mb[2] != PROD_ID_2 && mb[2] != PROD_ID_2a) ||
3287             mb[3] != PROD_ID_3) {
3288                 ql_log(ql_log_warn, vha, 0x0062,
3289                     "Wrong product ID = 0x%x,0x%x,0x%x.\n",
3290                     mb[1], mb[2], mb[3]);
3291
3292                 goto chip_diag_failed;
3293         }
3294         ha->product_id[0] = mb[1];
3295         ha->product_id[1] = mb[2];
3296         ha->product_id[2] = mb[3];
3297         ha->product_id[3] = mb[4];
3298
3299         /* Adjust fw RISC transfer size */
3300         if (req->length > 1024)
3301                 ha->fw_transfer_size = REQUEST_ENTRY_SIZE * 1024;
3302         else
3303                 ha->fw_transfer_size = REQUEST_ENTRY_SIZE *
3304                     req->length;
3305
3306         if (IS_QLA2200(ha) &&
3307             RD_MAILBOX_REG(ha, reg, 7) == QLA2200A_RISC_ROM_VER) {
3308                 /* Limit firmware transfer size with a 2200A */
3309                 ql_dbg(ql_dbg_init, vha, 0x007e, "Found QLA2200A Chip.\n");
3310
3311                 ha->device_type |= DT_ISP2200A;
3312                 ha->fw_transfer_size = 128;
3313         }
3314
3315         /* Wrap Incoming Mailboxes Test. */
3316         spin_unlock_irqrestore(&ha->hardware_lock, flags);
3317
3318         ql_dbg(ql_dbg_init, vha, 0x007f, "Checking mailboxes.\n");
3319         rval = qla2x00_mbx_reg_test(vha);
3320         if (rval)
3321                 ql_log(ql_log_warn, vha, 0x0080,
3322                     "Failed mailbox send register test.\n");
3323         else
3324                 /* Flag a successful rval */
3325                 rval = QLA_SUCCESS;
3326         spin_lock_irqsave(&ha->hardware_lock, flags);
3327
3328 chip_diag_failed:
3329         if (rval)
3330                 ql_log(ql_log_info, vha, 0x0081,
3331                     "Chip diagnostics **** FAILED ****.\n");
3332
3333         spin_unlock_irqrestore(&ha->hardware_lock, flags);
3334
3335         return (rval);
3336 }
3337
3338 /**
3339  * qla24xx_chip_diag() - Test ISP24xx for proper operation.
3340  * @vha: HA context
3341  *
3342  * Returns 0 on success.
3343  */
3344 int
3345 qla24xx_chip_diag(scsi_qla_host_t *vha)
3346 {
3347         int rval;
3348         struct qla_hw_data *ha = vha->hw;
3349         struct req_que *req = ha->req_q_map[0];
3350
3351         if (IS_P3P_TYPE(ha))
3352                 return QLA_SUCCESS;
3353
3354         ha->fw_transfer_size = REQUEST_ENTRY_SIZE * req->length;
3355
3356         rval = qla2x00_mbx_reg_test(vha);
3357         if (rval) {
3358                 ql_log(ql_log_warn, vha, 0x0082,
3359                     "Failed mailbox send register test.\n");
3360         } else {
3361                 /* Flag a successful rval */
3362                 rval = QLA_SUCCESS;
3363         }
3364
3365         return rval;
3366 }
3367
3368 static void
3369 qla2x00_init_fce_trace(scsi_qla_host_t *vha)
3370 {
3371         int rval;
3372         dma_addr_t tc_dma;
3373         void *tc;
3374         struct qla_hw_data *ha = vha->hw;
3375
3376         if (!IS_FWI2_CAPABLE(ha))
3377                 return;
3378
3379         if (!IS_QLA25XX(ha) && !IS_QLA81XX(ha) && !IS_QLA83XX(ha) &&
3380             !IS_QLA27XX(ha) && !IS_QLA28XX(ha))
3381                 return;
3382
3383         if (ha->fce) {
3384                 ql_dbg(ql_dbg_init, vha, 0x00bd,
3385                        "%s: FCE Mem is already allocated.\n",
3386                        __func__);
3387                 return;
3388         }
3389
3390         /* Allocate memory for Fibre Channel Event Buffer. */
3391         tc = dma_alloc_coherent(&ha->pdev->dev, FCE_SIZE, &tc_dma,
3392                                 GFP_KERNEL);
3393         if (!tc) {
3394                 ql_log(ql_log_warn, vha, 0x00be,
3395                        "Unable to allocate (%d KB) for FCE.\n",
3396                        FCE_SIZE / 1024);
3397                 return;
3398         }
3399
3400         rval = qla2x00_enable_fce_trace(vha, tc_dma, FCE_NUM_BUFFERS,
3401                                         ha->fce_mb, &ha->fce_bufs);
3402         if (rval) {
3403                 ql_log(ql_log_warn, vha, 0x00bf,
3404                        "Unable to initialize FCE (%d).\n", rval);
3405                 dma_free_coherent(&ha->pdev->dev, FCE_SIZE, tc, tc_dma);
3406                 return;
3407         }
3408
3409         ql_dbg(ql_dbg_init, vha, 0x00c0,
3410                "Allocated (%d KB) for FCE...\n", FCE_SIZE / 1024);
3411
3412         ha->flags.fce_enabled = 1;
3413         ha->fce_dma = tc_dma;
3414         ha->fce = tc;
3415 }
3416
3417 static void
3418 qla2x00_init_eft_trace(scsi_qla_host_t *vha)
3419 {
3420         int rval;
3421         dma_addr_t tc_dma;
3422         void *tc;
3423         struct qla_hw_data *ha = vha->hw;
3424
3425         if (!IS_FWI2_CAPABLE(ha))
3426                 return;
3427
3428         if (ha->eft) {
3429                 ql_dbg(ql_dbg_init, vha, 0x00bd,
3430                     "%s: EFT Mem is already allocated.\n",
3431                     __func__);
3432                 return;
3433         }
3434
3435         /* Allocate memory for Extended Trace Buffer. */
3436         tc = dma_alloc_coherent(&ha->pdev->dev, EFT_SIZE, &tc_dma,
3437                                 GFP_KERNEL);
3438         if (!tc) {
3439                 ql_log(ql_log_warn, vha, 0x00c1,
3440                        "Unable to allocate (%d KB) for EFT.\n",
3441                        EFT_SIZE / 1024);
3442                 return;
3443         }
3444
3445         rval = qla2x00_enable_eft_trace(vha, tc_dma, EFT_NUM_BUFFERS);
3446         if (rval) {
3447                 ql_log(ql_log_warn, vha, 0x00c2,
3448                        "Unable to initialize EFT (%d).\n", rval);
3449                 dma_free_coherent(&ha->pdev->dev, EFT_SIZE, tc, tc_dma);
3450                 return;
3451         }
3452
3453         ql_dbg(ql_dbg_init, vha, 0x00c3,
3454                "Allocated (%d KB) EFT ...\n", EFT_SIZE / 1024);
3455
3456         ha->eft_dma = tc_dma;
3457         ha->eft = tc;
3458 }
3459
3460 static void
3461 qla2x00_alloc_offload_mem(scsi_qla_host_t *vha)
3462 {
3463         qla2x00_init_fce_trace(vha);
3464         qla2x00_init_eft_trace(vha);
3465 }
3466
3467 void
3468 qla2x00_alloc_fw_dump(scsi_qla_host_t *vha)
3469 {
3470         uint32_t dump_size, fixed_size, mem_size, req_q_size, rsp_q_size,
3471             eft_size, fce_size, mq_size;
3472         struct qla_hw_data *ha = vha->hw;
3473         struct req_que *req = ha->req_q_map[0];
3474         struct rsp_que *rsp = ha->rsp_q_map[0];
3475         struct qla2xxx_fw_dump *fw_dump;
3476
3477         dump_size = fixed_size = mem_size = eft_size = fce_size = mq_size = 0;
3478         req_q_size = rsp_q_size = 0;
3479
3480         if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
3481                 fixed_size = sizeof(struct qla2100_fw_dump);
3482         } else if (IS_QLA23XX(ha)) {
3483                 fixed_size = offsetof(struct qla2300_fw_dump, data_ram);
3484                 mem_size = (ha->fw_memory_size - 0x11000 + 1) *
3485                     sizeof(uint16_t);
3486         } else if (IS_FWI2_CAPABLE(ha)) {
3487                 if (IS_QLA83XX(ha) || IS_QLA27XX(ha) || IS_QLA28XX(ha))
3488                         fixed_size = offsetof(struct qla83xx_fw_dump, ext_mem);
3489                 else if (IS_QLA81XX(ha))
3490                         fixed_size = offsetof(struct qla81xx_fw_dump, ext_mem);
3491                 else if (IS_QLA25XX(ha))
3492                         fixed_size = offsetof(struct qla25xx_fw_dump, ext_mem);
3493                 else
3494                         fixed_size = offsetof(struct qla24xx_fw_dump, ext_mem);
3495
3496                 mem_size = (ha->fw_memory_size - 0x100000 + 1) *
3497                     sizeof(uint32_t);
3498                 if (ha->mqenable) {
3499                         if (!IS_QLA83XX(ha) && !IS_QLA27XX(ha) &&
3500                             !IS_QLA28XX(ha))
3501                                 mq_size = sizeof(struct qla2xxx_mq_chain);
3502                         /*
3503                          * Allocate maximum buffer size for all queues - Q0.
3504                          * Resizing must be done at end-of-dump processing.
3505                          */
3506                         mq_size += (ha->max_req_queues - 1) *
3507                             (req->length * sizeof(request_t));
3508                         mq_size += (ha->max_rsp_queues - 1) *
3509                             (rsp->length * sizeof(response_t));
3510                 }
3511                 if (ha->tgt.atio_ring)
3512                         mq_size += ha->tgt.atio_q_length * sizeof(request_t);
3513
3514                 qla2x00_init_fce_trace(vha);
3515                 if (ha->fce)
3516                         fce_size = sizeof(struct qla2xxx_fce_chain) + FCE_SIZE;
3517                 qla2x00_init_eft_trace(vha);
3518                 if (ha->eft)
3519                         eft_size = EFT_SIZE;
3520         }
3521
3522         if (IS_QLA27XX(ha) || IS_QLA28XX(ha)) {
3523                 struct fwdt *fwdt = ha->fwdt;
3524                 uint j;
3525
3526                 for (j = 0; j < 2; j++, fwdt++) {
3527                         if (!fwdt->template) {
3528                                 ql_dbg(ql_dbg_init, vha, 0x00ba,
3529                                     "-> fwdt%u no template\n", j);
3530                                 continue;
3531                         }
3532                         ql_dbg(ql_dbg_init, vha, 0x00fa,
3533                             "-> fwdt%u calculating fwdump size...\n", j);
3534                         fwdt->dump_size = qla27xx_fwdt_calculate_dump_size(
3535                             vha, fwdt->template);
3536                         ql_dbg(ql_dbg_init, vha, 0x00fa,
3537                             "-> fwdt%u calculated fwdump size = %#lx bytes\n",
3538                             j, fwdt->dump_size);
3539                         dump_size += fwdt->dump_size;
3540                 }
3541                 /* Add space for spare MPI fw dump. */
3542                 dump_size += ha->fwdt[1].dump_size;
3543         } else {
3544                 req_q_size = req->length * sizeof(request_t);
3545                 rsp_q_size = rsp->length * sizeof(response_t);
3546                 dump_size = offsetof(struct qla2xxx_fw_dump, isp);
3547                 dump_size += fixed_size + mem_size + req_q_size + rsp_q_size
3548                         + eft_size;
3549                 ha->chain_offset = dump_size;
3550                 dump_size += mq_size + fce_size;
3551                 if (ha->exchoffld_buf)
3552                         dump_size += sizeof(struct qla2xxx_offld_chain) +
3553                                 ha->exchoffld_size;
3554                 if (ha->exlogin_buf)
3555                         dump_size += sizeof(struct qla2xxx_offld_chain) +
3556                                 ha->exlogin_size;
3557         }
3558
3559         if (!ha->fw_dump_len || dump_size > ha->fw_dump_alloc_len) {
3560
3561                 ql_dbg(ql_dbg_init, vha, 0x00c5,
3562                     "%s dump_size %d fw_dump_len %d fw_dump_alloc_len %d\n",
3563                     __func__, dump_size, ha->fw_dump_len,
3564                     ha->fw_dump_alloc_len);
3565
3566                 fw_dump = vmalloc(dump_size);
3567                 if (!fw_dump) {
3568                         ql_log(ql_log_warn, vha, 0x00c4,
3569                             "Unable to allocate (%d KB) for firmware dump.\n",
3570                             dump_size / 1024);
3571                 } else {
3572                         mutex_lock(&ha->optrom_mutex);
3573                         if (ha->fw_dumped) {
3574                                 memcpy(fw_dump, ha->fw_dump, ha->fw_dump_len);
3575                                 vfree(ha->fw_dump);
3576                                 ha->fw_dump = fw_dump;
3577                                 ha->fw_dump_alloc_len =  dump_size;
3578                                 ql_dbg(ql_dbg_init, vha, 0x00c5,
3579                                     "Re-Allocated (%d KB) and save firmware dump.\n",
3580                                     dump_size / 1024);
3581                         } else {
3582                                 vfree(ha->fw_dump);
3583                                 ha->fw_dump = fw_dump;
3584
3585                                 ha->fw_dump_len = ha->fw_dump_alloc_len =
3586                                     dump_size;
3587                                 ql_dbg(ql_dbg_init, vha, 0x00c5,
3588                                     "Allocated (%d KB) for firmware dump.\n",
3589                                     dump_size / 1024);
3590
3591                                 if (IS_QLA27XX(ha) || IS_QLA28XX(ha)) {
3592                                         ha->mpi_fw_dump = (char *)fw_dump +
3593                                                 ha->fwdt[1].dump_size;
3594                                         mutex_unlock(&ha->optrom_mutex);
3595                                         return;
3596                                 }
3597
3598                                 ha->fw_dump->signature[0] = 'Q';
3599                                 ha->fw_dump->signature[1] = 'L';
3600                                 ha->fw_dump->signature[2] = 'G';
3601                                 ha->fw_dump->signature[3] = 'C';
3602                                 ha->fw_dump->version = htonl(1);
3603
3604                                 ha->fw_dump->fixed_size = htonl(fixed_size);
3605                                 ha->fw_dump->mem_size = htonl(mem_size);
3606                                 ha->fw_dump->req_q_size = htonl(req_q_size);
3607                                 ha->fw_dump->rsp_q_size = htonl(rsp_q_size);
3608
3609                                 ha->fw_dump->eft_size = htonl(eft_size);
3610                                 ha->fw_dump->eft_addr_l =
3611                                     htonl(LSD(ha->eft_dma));
3612                                 ha->fw_dump->eft_addr_h =
3613                                     htonl(MSD(ha->eft_dma));
3614
3615                                 ha->fw_dump->header_size =
3616                                         htonl(offsetof
3617                                             (struct qla2xxx_fw_dump, isp));
3618                         }
3619                         mutex_unlock(&ha->optrom_mutex);
3620                 }
3621         }
3622 }
3623
3624 static int
3625 qla81xx_mpi_sync(scsi_qla_host_t *vha)
3626 {
3627 #define MPS_MASK        0xe0
3628         int rval;
3629         uint16_t dc;
3630         uint32_t dw;
3631
3632         if (!IS_QLA81XX(vha->hw))
3633                 return QLA_SUCCESS;
3634
3635         rval = qla2x00_write_ram_word(vha, 0x7c00, 1);
3636         if (rval != QLA_SUCCESS) {
3637                 ql_log(ql_log_warn, vha, 0x0105,
3638                     "Unable to acquire semaphore.\n");
3639                 goto done;
3640         }
3641
3642         pci_read_config_word(vha->hw->pdev, 0x54, &dc);
3643         rval = qla2x00_read_ram_word(vha, 0x7a15, &dw);
3644         if (rval != QLA_SUCCESS) {
3645                 ql_log(ql_log_warn, vha, 0x0067, "Unable to read sync.\n");
3646                 goto done_release;
3647         }
3648
3649         dc &= MPS_MASK;
3650         if (dc == (dw & MPS_MASK))
3651                 goto done_release;
3652
3653         dw &= ~MPS_MASK;
3654         dw |= dc;
3655         rval = qla2x00_write_ram_word(vha, 0x7a15, dw);
3656         if (rval != QLA_SUCCESS) {
3657                 ql_log(ql_log_warn, vha, 0x0114, "Unable to gain sync.\n");
3658         }
3659
3660 done_release:
3661         rval = qla2x00_write_ram_word(vha, 0x7c00, 0);
3662         if (rval != QLA_SUCCESS) {
3663                 ql_log(ql_log_warn, vha, 0x006d,
3664                     "Unable to release semaphore.\n");
3665         }
3666
3667 done:
3668         return rval;
3669 }
3670
3671 int
3672 qla2x00_alloc_outstanding_cmds(struct qla_hw_data *ha, struct req_que *req)
3673 {
3674         /* Don't try to reallocate the array */
3675         if (req->outstanding_cmds)
3676                 return QLA_SUCCESS;
3677
3678         if (!IS_FWI2_CAPABLE(ha))
3679                 req->num_outstanding_cmds = DEFAULT_OUTSTANDING_COMMANDS;
3680         else {
3681                 if (ha->cur_fw_xcb_count <= ha->cur_fw_iocb_count)
3682                         req->num_outstanding_cmds = ha->cur_fw_xcb_count;
3683                 else
3684                         req->num_outstanding_cmds = ha->cur_fw_iocb_count;
3685         }
3686
3687         req->outstanding_cmds = kcalloc(req->num_outstanding_cmds,
3688                                         sizeof(srb_t *),
3689                                         GFP_KERNEL);
3690
3691         if (!req->outstanding_cmds) {
3692                 /*
3693                  * Try to allocate a minimal size just so we can get through
3694                  * initialization.
3695                  */
3696                 req->num_outstanding_cmds = MIN_OUTSTANDING_COMMANDS;
3697                 req->outstanding_cmds = kcalloc(req->num_outstanding_cmds,
3698                                                 sizeof(srb_t *),
3699                                                 GFP_KERNEL);
3700
3701                 if (!req->outstanding_cmds) {
3702                         ql_log(ql_log_fatal, NULL, 0x0126,
3703                             "Failed to allocate memory for "
3704                             "outstanding_cmds for req_que %p.\n", req);
3705                         req->num_outstanding_cmds = 0;
3706                         return QLA_FUNCTION_FAILED;
3707                 }
3708         }
3709
3710         return QLA_SUCCESS;
3711 }
3712
3713 #define PRINT_FIELD(_field, _flag, _str) {              \
3714         if (a0->_field & _flag) {\
3715                 if (p) {\
3716                         strcat(ptr, "|");\
3717                         ptr++;\
3718                         leftover--;\
3719                 } \
3720                 len = snprintf(ptr, leftover, "%s", _str);      \
3721                 p = 1;\
3722                 leftover -= len;\
3723                 ptr += len; \
3724         } \
3725 }
3726
3727 static void qla2xxx_print_sfp_info(struct scsi_qla_host *vha)
3728 {
3729 #define STR_LEN 64
3730         struct sff_8247_a0 *a0 = (struct sff_8247_a0 *)vha->hw->sfp_data;
3731         u8 str[STR_LEN], *ptr, p;
3732         int leftover, len;
3733
3734         memset(str, 0, STR_LEN);
3735         snprintf(str, SFF_VEN_NAME_LEN+1, a0->vendor_name);
3736         ql_dbg(ql_dbg_init, vha, 0x015a,
3737             "SFP MFG Name: %s\n", str);
3738
3739         memset(str, 0, STR_LEN);
3740         snprintf(str, SFF_PART_NAME_LEN+1, a0->vendor_pn);
3741         ql_dbg(ql_dbg_init, vha, 0x015c,
3742             "SFP Part Name: %s\n", str);
3743
3744         /* media */
3745         memset(str, 0, STR_LEN);
3746         ptr = str;
3747         leftover = STR_LEN;
3748         p = len = 0;
3749         PRINT_FIELD(fc_med_cc9, FC_MED_TW, "Twin AX");
3750         PRINT_FIELD(fc_med_cc9, FC_MED_TP, "Twisted Pair");
3751         PRINT_FIELD(fc_med_cc9, FC_MED_MI, "Min Coax");
3752         PRINT_FIELD(fc_med_cc9, FC_MED_TV, "Video Coax");
3753         PRINT_FIELD(fc_med_cc9, FC_MED_M6, "MultiMode 62.5um");
3754         PRINT_FIELD(fc_med_cc9, FC_MED_M5, "MultiMode 50um");
3755         PRINT_FIELD(fc_med_cc9, FC_MED_SM, "SingleMode");
3756         ql_dbg(ql_dbg_init, vha, 0x0160,
3757             "SFP Media: %s\n", str);
3758
3759         /* link length */
3760         memset(str, 0, STR_LEN);
3761         ptr = str;
3762         leftover = STR_LEN;
3763         p = len = 0;
3764         PRINT_FIELD(fc_ll_cc7, FC_LL_VL, "Very Long");
3765         PRINT_FIELD(fc_ll_cc7, FC_LL_S, "Short");
3766         PRINT_FIELD(fc_ll_cc7, FC_LL_I, "Intermediate");
3767         PRINT_FIELD(fc_ll_cc7, FC_LL_L, "Long");
3768         PRINT_FIELD(fc_ll_cc7, FC_LL_M, "Medium");
3769         ql_dbg(ql_dbg_init, vha, 0x0196,
3770             "SFP Link Length: %s\n", str);
3771
3772         memset(str, 0, STR_LEN);
3773         ptr = str;
3774         leftover = STR_LEN;
3775         p = len = 0;
3776         PRINT_FIELD(fc_ll_cc7, FC_LL_SA, "Short Wave (SA)");
3777         PRINT_FIELD(fc_ll_cc7, FC_LL_LC, "Long Wave(LC)");
3778         PRINT_FIELD(fc_tec_cc8, FC_TEC_SN, "Short Wave (SN)");
3779         PRINT_FIELD(fc_tec_cc8, FC_TEC_SL, "Short Wave (SL)");
3780         PRINT_FIELD(fc_tec_cc8, FC_TEC_LL, "Long Wave (LL)");
3781         ql_dbg(ql_dbg_init, vha, 0x016e,
3782             "SFP FC Link Tech: %s\n", str);
3783
3784         if (a0->length_km)
3785                 ql_dbg(ql_dbg_init, vha, 0x016f,
3786                     "SFP Distant: %d km\n", a0->length_km);
3787         if (a0->length_100m)
3788                 ql_dbg(ql_dbg_init, vha, 0x0170,
3789                     "SFP Distant: %d m\n", a0->length_100m*100);
3790         if (a0->length_50um_10m)
3791                 ql_dbg(ql_dbg_init, vha, 0x0189,
3792                     "SFP Distant (WL=50um): %d m\n", a0->length_50um_10m * 10);
3793         if (a0->length_62um_10m)
3794                 ql_dbg(ql_dbg_init, vha, 0x018a,
3795                   "SFP Distant (WL=62.5um): %d m\n", a0->length_62um_10m * 10);
3796         if (a0->length_om4_10m)
3797                 ql_dbg(ql_dbg_init, vha, 0x0194,
3798                     "SFP Distant (OM4): %d m\n", a0->length_om4_10m * 10);
3799         if (a0->length_om3_10m)
3800                 ql_dbg(ql_dbg_init, vha, 0x0195,
3801                     "SFP Distant (OM3): %d m\n", a0->length_om3_10m * 10);
3802 }
3803
3804
3805 /**
3806  * qla24xx_detect_sfp()
3807  *
3808  * @vha: adapter state pointer.
3809  *
3810  * @return
3811  *      0 -- Configure firmware to use short-range settings -- normal
3812  *           buffer-to-buffer credits.
3813  *
3814  *      1 -- Configure firmware to use long-range settings -- extra
3815  *           buffer-to-buffer credits should be allocated with
3816  *           ha->lr_distance containing distance settings from NVRAM or SFP
3817  *           (if supported).
3818  */
3819 int
3820 qla24xx_detect_sfp(scsi_qla_host_t *vha)
3821 {
3822         int rc, used_nvram;
3823         struct sff_8247_a0 *a;
3824         struct qla_hw_data *ha = vha->hw;
3825         struct nvram_81xx *nv = ha->nvram;
3826 #define LR_DISTANCE_UNKNOWN     2
3827         static const char * const types[] = { "Short", "Long" };
3828         static const char * const lengths[] = { "(10km)", "(5km)", "" };
3829         u8 ll = 0;
3830
3831         /* Seed with NVRAM settings. */
3832         used_nvram = 0;
3833         ha->flags.lr_detected = 0;
3834         if (IS_BPM_RANGE_CAPABLE(ha) &&
3835             (nv->enhanced_features & NEF_LR_DIST_ENABLE)) {
3836                 used_nvram = 1;
3837                 ha->flags.lr_detected = 1;
3838                 ha->lr_distance =
3839                     (nv->enhanced_features >> LR_DIST_NV_POS)
3840                      & LR_DIST_NV_MASK;
3841         }
3842
3843         if (!IS_BPM_ENABLED(vha))
3844                 goto out;
3845         /* Determine SR/LR capabilities of SFP/Transceiver. */
3846         rc = qla2x00_read_sfp_dev(vha, NULL, 0);
3847         if (rc)
3848                 goto out;
3849
3850         used_nvram = 0;
3851         a = (struct sff_8247_a0 *)vha->hw->sfp_data;
3852         qla2xxx_print_sfp_info(vha);
3853
3854         ha->flags.lr_detected = 0;
3855         ll = a->fc_ll_cc7;
3856         if (ll & FC_LL_VL || ll & FC_LL_L) {
3857                 /* Long range, track length. */
3858                 ha->flags.lr_detected = 1;
3859
3860                 if (a->length_km > 5 || a->length_100m > 50)
3861                         ha->lr_distance = LR_DISTANCE_10K;
3862                 else
3863                         ha->lr_distance = LR_DISTANCE_5K;
3864         }
3865
3866 out:
3867         ql_dbg(ql_dbg_async, vha, 0x507b,
3868             "SFP detect: %s-Range SFP %s (nvr=%x ll=%x lr=%x lrd=%x).\n",
3869             types[ha->flags.lr_detected],
3870             ha->flags.lr_detected ? lengths[ha->lr_distance] :
3871                lengths[LR_DISTANCE_UNKNOWN],
3872             used_nvram, ll, ha->flags.lr_detected, ha->lr_distance);
3873         return ha->flags.lr_detected;
3874 }
3875
3876 void qla_init_iocb_limit(scsi_qla_host_t *vha)
3877 {
3878         u16 i, num_qps;
3879         u32 limit;
3880         struct qla_hw_data *ha = vha->hw;
3881
3882         num_qps = ha->num_qpairs + 1;
3883         limit = (ha->orig_fw_iocb_count * QLA_IOCB_PCT_LIMIT) / 100;
3884
3885         ha->base_qpair->fwres.iocbs_total = ha->orig_fw_iocb_count;
3886         ha->base_qpair->fwres.iocbs_limit = limit;
3887         ha->base_qpair->fwres.iocbs_qp_limit = limit / num_qps;
3888         ha->base_qpair->fwres.iocbs_used = 0;
3889         for (i = 0; i < ha->max_qpairs; i++) {
3890                 if (ha->queue_pair_map[i])  {
3891                         ha->queue_pair_map[i]->fwres.iocbs_total =
3892                                 ha->orig_fw_iocb_count;
3893                         ha->queue_pair_map[i]->fwres.iocbs_limit = limit;
3894                         ha->queue_pair_map[i]->fwres.iocbs_qp_limit =
3895                                 limit / num_qps;
3896                         ha->queue_pair_map[i]->fwres.iocbs_used = 0;
3897                 }
3898         }
3899 }
3900
3901 /**
3902  * qla2x00_setup_chip() - Load and start RISC firmware.
3903  * @vha: HA context
3904  *
3905  * Returns 0 on success.
3906  */
3907 static int
3908 qla2x00_setup_chip(scsi_qla_host_t *vha)
3909 {
3910         int rval;
3911         uint32_t srisc_address = 0;
3912         struct qla_hw_data *ha = vha->hw;
3913         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
3914         unsigned long flags;
3915         uint16_t fw_major_version;
3916         int done_once = 0;
3917
3918         if (IS_P3P_TYPE(ha)) {
3919                 rval = ha->isp_ops->load_risc(vha, &srisc_address);
3920                 if (rval == QLA_SUCCESS) {
3921                         qla2x00_stop_firmware(vha);
3922                         goto enable_82xx_npiv;
3923                 } else
3924                         goto failed;
3925         }
3926
3927         if (!IS_FWI2_CAPABLE(ha) && !IS_QLA2100(ha) && !IS_QLA2200(ha)) {
3928                 /* Disable SRAM, Instruction RAM and GP RAM parity.  */
3929                 spin_lock_irqsave(&ha->hardware_lock, flags);
3930                 wrt_reg_word(&reg->hccr, (HCCR_ENABLE_PARITY + 0x0));
3931                 rd_reg_word(&reg->hccr);
3932                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3933         }
3934
3935         qla81xx_mpi_sync(vha);
3936
3937 execute_fw_with_lr:
3938         /* Load firmware sequences */
3939         rval = ha->isp_ops->load_risc(vha, &srisc_address);
3940         if (rval == QLA_SUCCESS) {
3941                 ql_dbg(ql_dbg_init, vha, 0x00c9,
3942                     "Verifying Checksum of loaded RISC code.\n");
3943
3944                 rval = qla2x00_verify_checksum(vha, srisc_address);
3945                 if (rval == QLA_SUCCESS) {
3946                         /* Start firmware execution. */
3947                         ql_dbg(ql_dbg_init, vha, 0x00ca,
3948                             "Starting firmware.\n");
3949
3950                         if (ql2xexlogins)
3951                                 ha->flags.exlogins_enabled = 1;
3952
3953                         if (qla_is_exch_offld_enabled(vha))
3954                                 ha->flags.exchoffld_enabled = 1;
3955
3956                         rval = qla2x00_execute_fw(vha, srisc_address);
3957                         /* Retrieve firmware information. */
3958                         if (rval == QLA_SUCCESS) {
3959                                 /* Enable BPM support? */
3960                                 if (!done_once++ && qla24xx_detect_sfp(vha)) {
3961                                         ql_dbg(ql_dbg_init, vha, 0x00ca,
3962                                             "Re-starting firmware -- BPM.\n");
3963                                         /* Best-effort - re-init. */
3964                                         ha->isp_ops->reset_chip(vha);
3965                                         ha->isp_ops->chip_diag(vha);
3966                                         goto execute_fw_with_lr;
3967                                 }
3968
3969                                 if (IS_ZIO_THRESHOLD_CAPABLE(ha))
3970                                         qla27xx_set_zio_threshold(vha,
3971                                             ha->last_zio_threshold);
3972
3973                                 rval = qla2x00_set_exlogins_buffer(vha);
3974                                 if (rval != QLA_SUCCESS)
3975                                         goto failed;
3976
3977                                 rval = qla2x00_set_exchoffld_buffer(vha);
3978                                 if (rval != QLA_SUCCESS)
3979                                         goto failed;
3980
3981 enable_82xx_npiv:
3982                                 fw_major_version = ha->fw_major_version;
3983                                 if (IS_P3P_TYPE(ha))
3984                                         qla82xx_check_md_needed(vha);
3985                                 else
3986                                         rval = qla2x00_get_fw_version(vha);
3987                                 if (rval != QLA_SUCCESS)
3988                                         goto failed;
3989                                 ha->flags.npiv_supported = 0;
3990                                 if (IS_QLA2XXX_MIDTYPE(ha) &&
3991                                          (ha->fw_attributes & BIT_2)) {
3992                                         ha->flags.npiv_supported = 1;
3993                                         if ((!ha->max_npiv_vports) ||
3994                                             ((ha->max_npiv_vports + 1) %
3995                                             MIN_MULTI_ID_FABRIC))
3996                                                 ha->max_npiv_vports =
3997                                                     MIN_MULTI_ID_FABRIC - 1;
3998                                 }
3999                                 qla2x00_get_resource_cnts(vha);
4000                                 qla_init_iocb_limit(vha);
4001
4002                                 /*
4003                                  * Allocate the array of outstanding commands
4004                                  * now that we know the firmware resources.
4005                                  */
4006                                 rval = qla2x00_alloc_outstanding_cmds(ha,
4007                                     vha->req);
4008                                 if (rval != QLA_SUCCESS)
4009                                         goto failed;
4010
4011                                 if (!fw_major_version && !(IS_P3P_TYPE(ha)))
4012                                         qla2x00_alloc_offload_mem(vha);
4013
4014                                 if (ql2xallocfwdump && !(IS_P3P_TYPE(ha)))
4015                                         qla2x00_alloc_fw_dump(vha);
4016
4017                         } else {
4018                                 goto failed;
4019                         }
4020                 } else {
4021                         ql_log(ql_log_fatal, vha, 0x00cd,
4022                             "ISP Firmware failed checksum.\n");
4023                         goto failed;
4024                 }
4025
4026                 /* Enable PUREX PASSTHRU */
4027                 if (ql2xrdpenable || ha->flags.scm_supported_f ||
4028                     ha->flags.edif_enabled)
4029                         qla25xx_set_els_cmds_supported(vha);
4030         } else
4031                 goto failed;
4032
4033         if (!IS_FWI2_CAPABLE(ha) && !IS_QLA2100(ha) && !IS_QLA2200(ha)) {
4034                 /* Enable proper parity. */
4035                 spin_lock_irqsave(&ha->hardware_lock, flags);
4036                 if (IS_QLA2300(ha))
4037                         /* SRAM parity */
4038                         wrt_reg_word(&reg->hccr, HCCR_ENABLE_PARITY + 0x1);
4039                 else
4040                         /* SRAM, Instruction RAM and GP RAM parity */
4041                         wrt_reg_word(&reg->hccr, HCCR_ENABLE_PARITY + 0x7);
4042                 rd_reg_word(&reg->hccr);
4043                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
4044         }
4045
4046         if (IS_QLA27XX(ha) || IS_QLA28XX(ha))
4047                 ha->flags.fac_supported = 1;
4048         else if (rval == QLA_SUCCESS && IS_FAC_REQUIRED(ha)) {
4049                 uint32_t size;
4050
4051                 rval = qla81xx_fac_get_sector_size(vha, &size);
4052                 if (rval == QLA_SUCCESS) {
4053                         ha->flags.fac_supported = 1;
4054                         ha->fdt_block_size = size << 2;
4055                 } else {
4056                         ql_log(ql_log_warn, vha, 0x00ce,
4057                             "Unsupported FAC firmware (%d.%02d.%02d).\n",
4058                             ha->fw_major_version, ha->fw_minor_version,
4059                             ha->fw_subminor_version);
4060
4061                         if (IS_QLA83XX(ha) || IS_QLA27XX(ha) ||
4062                             IS_QLA28XX(ha)) {
4063                                 ha->flags.fac_supported = 0;
4064                                 rval = QLA_SUCCESS;
4065                         }
4066                 }
4067         }
4068 failed:
4069         if (rval) {
4070                 ql_log(ql_log_fatal, vha, 0x00cf,
4071                     "Setup chip ****FAILED****.\n");
4072         }
4073
4074         return (rval);
4075 }
4076
4077 /**
4078  * qla2x00_init_response_q_entries() - Initializes response queue entries.
4079  * @rsp: response queue
4080  *
4081  * Beginning of request ring has initialization control block already built
4082  * by nvram config routine.
4083  *
4084  * Returns 0 on success.
4085  */
4086 void
4087 qla2x00_init_response_q_entries(struct rsp_que *rsp)
4088 {
4089         uint16_t cnt;
4090         response_t *pkt;
4091
4092         rsp->ring_ptr = rsp->ring;
4093         rsp->ring_index    = 0;
4094         rsp->status_srb = NULL;
4095         pkt = rsp->ring_ptr;
4096         for (cnt = 0; cnt < rsp->length; cnt++) {
4097                 pkt->signature = RESPONSE_PROCESSED;
4098                 pkt++;
4099         }
4100 }
4101
4102 /**
4103  * qla2x00_update_fw_options() - Read and process firmware options.
4104  * @vha: HA context
4105  *
4106  * Returns 0 on success.
4107  */
4108 void
4109 qla2x00_update_fw_options(scsi_qla_host_t *vha)
4110 {
4111         uint16_t swing, emphasis, tx_sens, rx_sens;
4112         struct qla_hw_data *ha = vha->hw;
4113
4114         memset(ha->fw_options, 0, sizeof(ha->fw_options));
4115         qla2x00_get_fw_options(vha, ha->fw_options);
4116
4117         if (IS_QLA2100(ha) || IS_QLA2200(ha))
4118                 return;
4119
4120         /* Serial Link options. */
4121         ql_dbg(ql_dbg_init + ql_dbg_buffer, vha, 0x0115,
4122             "Serial link options.\n");
4123         ql_dump_buffer(ql_dbg_init + ql_dbg_buffer, vha, 0x0109,
4124             ha->fw_seriallink_options, sizeof(ha->fw_seriallink_options));
4125
4126         ha->fw_options[1] &= ~FO1_SET_EMPHASIS_SWING;
4127         if (ha->fw_seriallink_options[3] & BIT_2) {
4128                 ha->fw_options[1] |= FO1_SET_EMPHASIS_SWING;
4129
4130                 /*  1G settings */
4131                 swing = ha->fw_seriallink_options[2] & (BIT_2 | BIT_1 | BIT_0);
4132                 emphasis = (ha->fw_seriallink_options[2] &
4133                     (BIT_4 | BIT_3)) >> 3;
4134                 tx_sens = ha->fw_seriallink_options[0] &
4135                     (BIT_3 | BIT_2 | BIT_1 | BIT_0);
4136                 rx_sens = (ha->fw_seriallink_options[0] &
4137                     (BIT_7 | BIT_6 | BIT_5 | BIT_4)) >> 4;
4138                 ha->fw_options[10] = (emphasis << 14) | (swing << 8);
4139                 if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
4140                         if (rx_sens == 0x0)
4141                                 rx_sens = 0x3;
4142                         ha->fw_options[10] |= (tx_sens << 4) | rx_sens;
4143                 } else if (IS_QLA2322(ha) || IS_QLA6322(ha))
4144                         ha->fw_options[10] |= BIT_5 |
4145                             ((rx_sens & (BIT_1 | BIT_0)) << 2) |
4146                             (tx_sens & (BIT_1 | BIT_0));
4147
4148                 /*  2G settings */
4149                 swing = (ha->fw_seriallink_options[2] &
4150                     (BIT_7 | BIT_6 | BIT_5)) >> 5;
4151                 emphasis = ha->fw_seriallink_options[3] & (BIT_1 | BIT_0);
4152                 tx_sens = ha->fw_seriallink_options[1] &
4153                     (BIT_3 | BIT_2 | BIT_1 | BIT_0);
4154                 rx_sens = (ha->fw_seriallink_options[1] &
4155                     (BIT_7 | BIT_6 | BIT_5 | BIT_4)) >> 4;
4156                 ha->fw_options[11] = (emphasis << 14) | (swing << 8);
4157                 if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
4158                         if (rx_sens == 0x0)
4159                                 rx_sens = 0x3;
4160                         ha->fw_options[11] |= (tx_sens << 4) | rx_sens;
4161                 } else if (IS_QLA2322(ha) || IS_QLA6322(ha))
4162                         ha->fw_options[11] |= BIT_5 |
4163                             ((rx_sens & (BIT_1 | BIT_0)) << 2) |
4164                             (tx_sens & (BIT_1 | BIT_0));
4165         }
4166
4167         /* FCP2 options. */
4168         /*  Return command IOCBs without waiting for an ABTS to complete. */
4169         ha->fw_options[3] |= BIT_13;
4170
4171         /* LED scheme. */
4172         if (ha->flags.enable_led_scheme)
4173                 ha->fw_options[2] |= BIT_12;
4174
4175         /* Detect ISP6312. */
4176         if (IS_QLA6312(ha))
4177                 ha->fw_options[2] |= BIT_13;
4178
4179         /* Set Retry FLOGI in case of P2P connection */
4180         if (ha->operating_mode == P2P) {
4181                 ha->fw_options[2] |= BIT_3;
4182                 ql_dbg(ql_dbg_disc, vha, 0x2100,
4183                     "(%s): Setting FLOGI retry BIT in fw_options[2]: 0x%x\n",
4184                         __func__, ha->fw_options[2]);
4185         }
4186
4187         /* Update firmware options. */
4188         qla2x00_set_fw_options(vha, ha->fw_options);
4189 }
4190
4191 void
4192 qla24xx_update_fw_options(scsi_qla_host_t *vha)
4193 {
4194         int rval;
4195         struct qla_hw_data *ha = vha->hw;
4196
4197         if (IS_P3P_TYPE(ha))
4198                 return;
4199
4200         /*  Hold status IOCBs until ABTS response received. */
4201         if (ql2xfwholdabts)
4202                 ha->fw_options[3] |= BIT_12;
4203
4204         /* Set Retry FLOGI in case of P2P connection */
4205         if (ha->operating_mode == P2P) {
4206                 ha->fw_options[2] |= BIT_3;
4207                 ql_dbg(ql_dbg_disc, vha, 0x2101,
4208                     "(%s): Setting FLOGI retry BIT in fw_options[2]: 0x%x\n",
4209                         __func__, ha->fw_options[2]);
4210         }
4211
4212         /* Move PUREX, ABTS RX & RIDA to ATIOQ */
4213         if (ql2xmvasynctoatio && !ha->flags.edif_enabled &&
4214             (IS_QLA83XX(ha) || IS_QLA27XX(ha) || IS_QLA28XX(ha))) {
4215                 if (qla_tgt_mode_enabled(vha) ||
4216                     qla_dual_mode_enabled(vha))
4217                         ha->fw_options[2] |= BIT_11;
4218                 else
4219                         ha->fw_options[2] &= ~BIT_11;
4220         }
4221
4222         if (IS_QLA25XX(ha) || IS_QLA83XX(ha) || IS_QLA27XX(ha) ||
4223             IS_QLA28XX(ha)) {
4224                 /*
4225                  * Tell FW to track each exchange to prevent
4226                  * driver from using stale exchange.
4227                  */
4228                 if (qla_tgt_mode_enabled(vha) ||
4229                     qla_dual_mode_enabled(vha))
4230                         ha->fw_options[2] |= BIT_4;
4231                 else
4232                         ha->fw_options[2] &= ~(BIT_4);
4233
4234                 /* Reserve 1/2 of emergency exchanges for ELS.*/
4235                 if (qla2xuseresexchforels)
4236                         ha->fw_options[2] |= BIT_8;
4237                 else
4238                         ha->fw_options[2] &= ~BIT_8;
4239
4240                 /*
4241                  * N2N: set Secure=1 for PLOGI ACC and
4242                  * fw shal not send PRLI after PLOGI Acc
4243                  */
4244                 if (ha->flags.edif_enabled &&
4245                     DBELL_ACTIVE(vha)) {
4246                         ha->fw_options[3] |= BIT_15;
4247                         ha->flags.n2n_fw_acc_sec = 1;
4248                 } else {
4249                         ha->fw_options[3] &= ~BIT_15;
4250                         ha->flags.n2n_fw_acc_sec = 0;
4251                 }
4252         }
4253
4254         if (ql2xrdpenable || ha->flags.scm_supported_f ||
4255             ha->flags.edif_enabled)
4256                 ha->fw_options[1] |= ADD_FO1_ENABLE_PUREX_IOCB;
4257
4258         /* Enable Async 8130/8131 events -- transceiver insertion/removal */
4259         if (IS_BPM_RANGE_CAPABLE(ha))
4260                 ha->fw_options[3] |= BIT_10;
4261
4262         ql_dbg(ql_dbg_init, vha, 0x00e8,
4263             "%s, add FW options 1-3 = 0x%04x 0x%04x 0x%04x mode %x\n",
4264             __func__, ha->fw_options[1], ha->fw_options[2],
4265             ha->fw_options[3], vha->host->active_mode);
4266
4267         if (ha->fw_options[1] || ha->fw_options[2] || ha->fw_options[3])
4268                 qla2x00_set_fw_options(vha, ha->fw_options);
4269
4270         /* Update Serial Link options. */
4271         if ((le16_to_cpu(ha->fw_seriallink_options24[0]) & BIT_0) == 0)
4272                 return;
4273
4274         rval = qla2x00_set_serdes_params(vha,
4275             le16_to_cpu(ha->fw_seriallink_options24[1]),
4276             le16_to_cpu(ha->fw_seriallink_options24[2]),
4277             le16_to_cpu(ha->fw_seriallink_options24[3]));
4278         if (rval != QLA_SUCCESS) {
4279                 ql_log(ql_log_warn, vha, 0x0104,
4280                     "Unable to update Serial Link options (%x).\n", rval);
4281         }
4282 }
4283
4284 void
4285 qla2x00_config_rings(struct scsi_qla_host *vha)
4286 {
4287         struct qla_hw_data *ha = vha->hw;
4288         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
4289         struct req_que *req = ha->req_q_map[0];
4290         struct rsp_que *rsp = ha->rsp_q_map[0];
4291
4292         /* Setup ring parameters in initialization control block. */
4293         ha->init_cb->request_q_outpointer = cpu_to_le16(0);
4294         ha->init_cb->response_q_inpointer = cpu_to_le16(0);
4295         ha->init_cb->request_q_length = cpu_to_le16(req->length);
4296         ha->init_cb->response_q_length = cpu_to_le16(rsp->length);
4297         put_unaligned_le64(req->dma, &ha->init_cb->request_q_address);
4298         put_unaligned_le64(rsp->dma, &ha->init_cb->response_q_address);
4299
4300         wrt_reg_word(ISP_REQ_Q_IN(ha, reg), 0);
4301         wrt_reg_word(ISP_REQ_Q_OUT(ha, reg), 0);
4302         wrt_reg_word(ISP_RSP_Q_IN(ha, reg), 0);
4303         wrt_reg_word(ISP_RSP_Q_OUT(ha, reg), 0);
4304         rd_reg_word(ISP_RSP_Q_OUT(ha, reg));            /* PCI Posting. */
4305 }
4306
4307 void
4308 qla24xx_config_rings(struct scsi_qla_host *vha)
4309 {
4310         struct qla_hw_data *ha = vha->hw;
4311         device_reg_t *reg = ISP_QUE_REG(ha, 0);
4312         struct device_reg_2xxx __iomem *ioreg = &ha->iobase->isp;
4313         struct qla_msix_entry *msix;
4314         struct init_cb_24xx *icb;
4315         uint16_t rid = 0;
4316         struct req_que *req = ha->req_q_map[0];
4317         struct rsp_que *rsp = ha->rsp_q_map[0];
4318
4319         /* Setup ring parameters in initialization control block. */
4320         icb = (struct init_cb_24xx *)ha->init_cb;
4321         icb->request_q_outpointer = cpu_to_le16(0);
4322         icb->response_q_inpointer = cpu_to_le16(0);
4323         icb->request_q_length = cpu_to_le16(req->length);
4324         icb->response_q_length = cpu_to_le16(rsp->length);
4325         put_unaligned_le64(req->dma, &icb->request_q_address);
4326         put_unaligned_le64(rsp->dma, &icb->response_q_address);
4327
4328         /* Setup ATIO queue dma pointers for target mode */
4329         icb->atio_q_inpointer = cpu_to_le16(0);
4330         icb->atio_q_length = cpu_to_le16(ha->tgt.atio_q_length);
4331         put_unaligned_le64(ha->tgt.atio_dma, &icb->atio_q_address);
4332
4333         if (IS_SHADOW_REG_CAPABLE(ha))
4334                 icb->firmware_options_2 |= cpu_to_le32(BIT_30|BIT_29);
4335
4336         if (ha->mqenable || IS_QLA83XX(ha) || IS_QLA27XX(ha) ||
4337             IS_QLA28XX(ha)) {
4338                 icb->qos = cpu_to_le16(QLA_DEFAULT_QUE_QOS);
4339                 icb->rid = cpu_to_le16(rid);
4340                 if (ha->flags.msix_enabled) {
4341                         msix = &ha->msix_entries[1];
4342                         ql_dbg(ql_dbg_init, vha, 0x0019,
4343                             "Registering vector 0x%x for base que.\n",
4344                             msix->entry);
4345                         icb->msix = cpu_to_le16(msix->entry);
4346                 }
4347                 /* Use alternate PCI bus number */
4348                 if (MSB(rid))
4349                         icb->firmware_options_2 |= cpu_to_le32(BIT_19);
4350                 /* Use alternate PCI devfn */
4351                 if (LSB(rid))
4352                         icb->firmware_options_2 |= cpu_to_le32(BIT_18);
4353
4354                 /* Use Disable MSIX Handshake mode for capable adapters */
4355                 if ((ha->fw_attributes & BIT_6) && (IS_MSIX_NACK_CAPABLE(ha)) &&
4356                     (ha->flags.msix_enabled)) {
4357                         icb->firmware_options_2 &= cpu_to_le32(~BIT_22);
4358                         ha->flags.disable_msix_handshake = 1;
4359                         ql_dbg(ql_dbg_init, vha, 0x00fe,
4360                             "MSIX Handshake Disable Mode turned on.\n");
4361                 } else {
4362                         icb->firmware_options_2 |= cpu_to_le32(BIT_22);
4363                 }
4364                 icb->firmware_options_2 |= cpu_to_le32(BIT_23);
4365
4366                 wrt_reg_dword(&reg->isp25mq.req_q_in, 0);
4367                 wrt_reg_dword(&reg->isp25mq.req_q_out, 0);
4368                 wrt_reg_dword(&reg->isp25mq.rsp_q_in, 0);
4369                 wrt_reg_dword(&reg->isp25mq.rsp_q_out, 0);
4370         } else {
4371                 wrt_reg_dword(&reg->isp24.req_q_in, 0);
4372                 wrt_reg_dword(&reg->isp24.req_q_out, 0);
4373                 wrt_reg_dword(&reg->isp24.rsp_q_in, 0);
4374                 wrt_reg_dword(&reg->isp24.rsp_q_out, 0);
4375         }
4376
4377         qlt_24xx_config_rings(vha);
4378
4379         /* If the user has configured the speed, set it here */
4380         if (ha->set_data_rate) {
4381                 ql_dbg(ql_dbg_init, vha, 0x00fd,
4382                     "Speed set by user : %s Gbps \n",
4383                     qla2x00_get_link_speed_str(ha, ha->set_data_rate));
4384                 icb->firmware_options_3 = cpu_to_le32(ha->set_data_rate << 13);
4385         }
4386
4387         /* PCI posting */
4388         rd_reg_word(&ioreg->hccr);
4389 }
4390
4391 /**
4392  * qla2x00_init_rings() - Initializes firmware.
4393  * @vha: HA context
4394  *
4395  * Beginning of request ring has initialization control block already built
4396  * by nvram config routine.
4397  *
4398  * Returns 0 on success.
4399  */
4400 int
4401 qla2x00_init_rings(scsi_qla_host_t *vha)
4402 {
4403         int     rval;
4404         unsigned long flags = 0;
4405         int cnt, que;
4406         struct qla_hw_data *ha = vha->hw;
4407         struct req_que *req;
4408         struct rsp_que *rsp;
4409         struct mid_init_cb_24xx *mid_init_cb =
4410             (struct mid_init_cb_24xx *) ha->init_cb;
4411
4412         spin_lock_irqsave(&ha->hardware_lock, flags);
4413
4414         /* Clear outstanding commands array. */
4415         for (que = 0; que < ha->max_req_queues; que++) {
4416                 req = ha->req_q_map[que];
4417                 if (!req || !test_bit(que, ha->req_qid_map))
4418                         continue;
4419                 req->out_ptr = (uint16_t *)(req->ring + req->length);
4420                 *req->out_ptr = 0;
4421                 for (cnt = 1; cnt < req->num_outstanding_cmds; cnt++)
4422                         req->outstanding_cmds[cnt] = NULL;
4423
4424                 req->current_outstanding_cmd = 1;
4425
4426                 /* Initialize firmware. */
4427                 req->ring_ptr  = req->ring;
4428                 req->ring_index    = 0;
4429                 req->cnt      = req->length;
4430         }
4431
4432         for (que = 0; que < ha->max_rsp_queues; que++) {
4433                 rsp = ha->rsp_q_map[que];
4434                 if (!rsp || !test_bit(que, ha->rsp_qid_map))
4435                         continue;
4436                 rsp->in_ptr = (uint16_t *)(rsp->ring + rsp->length);
4437                 *rsp->in_ptr = 0;
4438                 /* Initialize response queue entries */
4439                 if (IS_QLAFX00(ha))
4440                         qlafx00_init_response_q_entries(rsp);
4441                 else
4442                         qla2x00_init_response_q_entries(rsp);
4443         }
4444
4445         ha->tgt.atio_ring_ptr = ha->tgt.atio_ring;
4446         ha->tgt.atio_ring_index = 0;
4447         /* Initialize ATIO queue entries */
4448         qlt_init_atio_q_entries(vha);
4449
4450         ha->isp_ops->config_rings(vha);
4451
4452         spin_unlock_irqrestore(&ha->hardware_lock, flags);
4453
4454         if (IS_QLAFX00(ha)) {
4455                 rval = qlafx00_init_firmware(vha, ha->init_cb_size);
4456                 goto next_check;
4457         }
4458
4459         /* Update any ISP specific firmware options before initialization. */
4460         ha->isp_ops->update_fw_options(vha);
4461
4462         ql_dbg(ql_dbg_init, vha, 0x00d1,
4463                "Issue init firmware FW opt 1-3= %08x %08x %08x.\n",
4464                le32_to_cpu(mid_init_cb->init_cb.firmware_options_1),
4465                le32_to_cpu(mid_init_cb->init_cb.firmware_options_2),
4466                le32_to_cpu(mid_init_cb->init_cb.firmware_options_3));
4467
4468         if (ha->flags.npiv_supported) {
4469                 if (ha->operating_mode == LOOP && !IS_CNA_CAPABLE(ha))
4470                         ha->max_npiv_vports = MIN_MULTI_ID_FABRIC - 1;
4471                 mid_init_cb->count = cpu_to_le16(ha->max_npiv_vports);
4472         }
4473
4474         if (IS_FWI2_CAPABLE(ha)) {
4475                 mid_init_cb->options = cpu_to_le16(BIT_1);
4476                 mid_init_cb->init_cb.execution_throttle =
4477                     cpu_to_le16(ha->cur_fw_xcb_count);
4478                 ha->flags.dport_enabled =
4479                         (le32_to_cpu(mid_init_cb->init_cb.firmware_options_1) &
4480                          BIT_7) != 0;
4481                 ql_dbg(ql_dbg_init, vha, 0x0191, "DPORT Support: %s.\n",
4482                     (ha->flags.dport_enabled) ? "enabled" : "disabled");
4483                 /* FA-WWPN Status */
4484                 ha->flags.fawwpn_enabled =
4485                         (le32_to_cpu(mid_init_cb->init_cb.firmware_options_1) &
4486                          BIT_6) != 0;
4487                 ql_dbg(ql_dbg_init, vha, 0x00bc, "FA-WWPN Support: %s.\n",
4488                     (ha->flags.fawwpn_enabled) ? "enabled" : "disabled");
4489         }
4490
4491         /* ELS pass through payload is limit by frame size. */
4492         if (ha->flags.edif_enabled)
4493                 mid_init_cb->init_cb.frame_payload_size = cpu_to_le16(ELS_MAX_PAYLOAD);
4494
4495         rval = qla2x00_init_firmware(vha, ha->init_cb_size);
4496 next_check:
4497         if (rval) {
4498                 ql_log(ql_log_fatal, vha, 0x00d2,
4499                     "Init Firmware **** FAILED ****.\n");
4500         } else {
4501                 ql_dbg(ql_dbg_init, vha, 0x00d3,
4502                     "Init Firmware -- success.\n");
4503                 QLA_FW_STARTED(ha);
4504                 vha->u_ql2xexchoffld = vha->u_ql2xiniexchg = 0;
4505         }
4506
4507         return (rval);
4508 }
4509
4510 /**
4511  * qla2x00_fw_ready() - Waits for firmware ready.
4512  * @vha: HA context
4513  *
4514  * Returns 0 on success.
4515  */
4516 static int
4517 qla2x00_fw_ready(scsi_qla_host_t *vha)
4518 {
4519         int             rval;
4520         unsigned long   wtime, mtime, cs84xx_time;
4521         uint16_t        min_wait;       /* Minimum wait time if loop is down */
4522         uint16_t        wait_time;      /* Wait time if loop is coming ready */
4523         uint16_t        state[6];
4524         struct qla_hw_data *ha = vha->hw;
4525
4526         if (IS_QLAFX00(vha->hw))
4527                 return qlafx00_fw_ready(vha);
4528
4529         /* Time to wait for loop down */
4530         if (IS_P3P_TYPE(ha))
4531                 min_wait = 30;
4532         else
4533                 min_wait = 20;
4534
4535         /*
4536          * Firmware should take at most one RATOV to login, plus 5 seconds for
4537          * our own processing.
4538          */
4539         if ((wait_time = (ha->retry_count*ha->login_timeout) + 5) < min_wait) {
4540                 wait_time = min_wait;
4541         }
4542
4543         /* Min wait time if loop down */
4544         mtime = jiffies + (min_wait * HZ);
4545
4546         /* wait time before firmware ready */
4547         wtime = jiffies + (wait_time * HZ);
4548
4549         /* Wait for ISP to finish LIP */
4550         if (!vha->flags.init_done)
4551                 ql_log(ql_log_info, vha, 0x801e,
4552                     "Waiting for LIP to complete.\n");
4553
4554         do {
4555                 memset(state, -1, sizeof(state));
4556                 rval = qla2x00_get_firmware_state(vha, state);
4557                 if (rval == QLA_SUCCESS) {
4558                         if (state[0] < FSTATE_LOSS_OF_SYNC) {
4559                                 vha->device_flags &= ~DFLG_NO_CABLE;
4560                         }
4561                         if (IS_QLA84XX(ha) && state[0] != FSTATE_READY) {
4562                                 ql_dbg(ql_dbg_taskm, vha, 0x801f,
4563                                     "fw_state=%x 84xx=%x.\n", state[0],
4564                                     state[2]);
4565                                 if ((state[2] & FSTATE_LOGGED_IN) &&
4566                                      (state[2] & FSTATE_WAITING_FOR_VERIFY)) {
4567                                         ql_dbg(ql_dbg_taskm, vha, 0x8028,
4568                                             "Sending verify iocb.\n");
4569
4570                                         cs84xx_time = jiffies;
4571                                         rval = qla84xx_init_chip(vha);
4572                                         if (rval != QLA_SUCCESS) {
4573                                                 ql_log(ql_log_warn,
4574                                                     vha, 0x8007,
4575                                                     "Init chip failed.\n");
4576                                                 break;
4577                                         }
4578
4579                                         /* Add time taken to initialize. */
4580                                         cs84xx_time = jiffies - cs84xx_time;
4581                                         wtime += cs84xx_time;
4582                                         mtime += cs84xx_time;
4583                                         ql_dbg(ql_dbg_taskm, vha, 0x8008,
4584                                             "Increasing wait time by %ld. "
4585                                             "New time %ld.\n", cs84xx_time,
4586                                             wtime);
4587                                 }
4588                         } else if (state[0] == FSTATE_READY) {
4589                                 ql_dbg(ql_dbg_taskm, vha, 0x8037,
4590                                     "F/W Ready - OK.\n");
4591
4592                                 qla2x00_get_retry_cnt(vha, &ha->retry_count,
4593                                     &ha->login_timeout, &ha->r_a_tov);
4594
4595                                 rval = QLA_SUCCESS;
4596                                 break;
4597                         }
4598
4599                         rval = QLA_FUNCTION_FAILED;
4600
4601                         if (atomic_read(&vha->loop_down_timer) &&
4602                             state[0] != FSTATE_READY) {
4603                                 /* Loop down. Timeout on min_wait for states
4604                                  * other than Wait for Login.
4605                                  */
4606                                 if (time_after_eq(jiffies, mtime)) {
4607                                         ql_log(ql_log_info, vha, 0x8038,
4608                                             "Cable is unplugged...\n");
4609
4610                                         vha->device_flags |= DFLG_NO_CABLE;
4611                                         break;
4612                                 }
4613                         }
4614                 } else {
4615                         /* Mailbox cmd failed. Timeout on min_wait. */
4616                         if (time_after_eq(jiffies, mtime) ||
4617                                 ha->flags.isp82xx_fw_hung)
4618                                 break;
4619                 }
4620
4621                 if (time_after_eq(jiffies, wtime))
4622                         break;
4623
4624                 /* Delay for a while */
4625                 msleep(500);
4626         } while (1);
4627
4628         ql_dbg(ql_dbg_taskm, vha, 0x803a,
4629             "fw_state=%x (%x, %x, %x, %x %x) curr time=%lx.\n", state[0],
4630             state[1], state[2], state[3], state[4], state[5], jiffies);
4631
4632         if (rval && !(vha->device_flags & DFLG_NO_CABLE)) {
4633                 ql_log(ql_log_warn, vha, 0x803b,
4634                     "Firmware ready **** FAILED ****.\n");
4635         }
4636
4637         return (rval);
4638 }
4639
4640 /*
4641 *  qla2x00_configure_hba
4642 *      Setup adapter context.
4643 *
4644 * Input:
4645 *      ha = adapter state pointer.
4646 *
4647 * Returns:
4648 *      0 = success
4649 *
4650 * Context:
4651 *      Kernel context.
4652 */
4653 static int
4654 qla2x00_configure_hba(scsi_qla_host_t *vha)
4655 {
4656         int       rval;
4657         uint16_t      loop_id;
4658         uint16_t      topo;
4659         uint16_t      sw_cap;
4660         uint8_t       al_pa;
4661         uint8_t       area;
4662         uint8_t       domain;
4663         char            connect_type[22];
4664         struct qla_hw_data *ha = vha->hw;
4665         scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
4666         port_id_t id;
4667         unsigned long flags;
4668
4669         /* Get host addresses. */
4670         rval = qla2x00_get_adapter_id(vha,
4671             &loop_id, &al_pa, &area, &domain, &topo, &sw_cap);
4672         if (rval != QLA_SUCCESS) {
4673                 if (LOOP_TRANSITION(vha) || atomic_read(&ha->loop_down_timer) ||
4674                     IS_CNA_CAPABLE(ha) ||
4675                     (rval == QLA_COMMAND_ERROR && loop_id == 0x7)) {
4676                         ql_dbg(ql_dbg_disc, vha, 0x2008,
4677                             "Loop is in a transition state.\n");
4678                 } else {
4679                         ql_log(ql_log_warn, vha, 0x2009,
4680                             "Unable to get host loop ID.\n");
4681                         if (IS_FWI2_CAPABLE(ha) && (vha == base_vha) &&
4682                             (rval == QLA_COMMAND_ERROR && loop_id == 0x1b)) {
4683                                 ql_log(ql_log_warn, vha, 0x1151,
4684                                     "Doing link init.\n");
4685                                 if (qla24xx_link_initialize(vha) == QLA_SUCCESS)
4686                                         return rval;
4687                         }
4688                         set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
4689                 }
4690                 return (rval);
4691         }
4692
4693         if (topo == 4) {
4694                 ql_log(ql_log_info, vha, 0x200a,
4695                     "Cannot get topology - retrying.\n");
4696                 return (QLA_FUNCTION_FAILED);
4697         }
4698
4699         vha->loop_id = loop_id;
4700
4701         /* initialize */
4702         ha->min_external_loopid = SNS_FIRST_LOOP_ID;
4703         ha->operating_mode = LOOP;
4704
4705         switch (topo) {
4706         case 0:
4707                 ql_dbg(ql_dbg_disc, vha, 0x200b, "HBA in NL topology.\n");
4708                 ha->switch_cap = 0;
4709                 ha->current_topology = ISP_CFG_NL;
4710                 strcpy(connect_type, "(Loop)");
4711                 break;
4712
4713         case 1:
4714                 ql_dbg(ql_dbg_disc, vha, 0x200c, "HBA in FL topology.\n");
4715                 ha->switch_cap = sw_cap;
4716                 ha->current_topology = ISP_CFG_FL;
4717                 strcpy(connect_type, "(FL_Port)");
4718                 break;
4719
4720         case 2:
4721                 ql_dbg(ql_dbg_disc, vha, 0x200d, "HBA in N P2P topology.\n");
4722                 ha->switch_cap = 0;
4723                 ha->operating_mode = P2P;
4724                 ha->current_topology = ISP_CFG_N;
4725                 strcpy(connect_type, "(N_Port-to-N_Port)");
4726                 break;
4727
4728         case 3:
4729                 ql_dbg(ql_dbg_disc, vha, 0x200e, "HBA in F P2P topology.\n");
4730                 ha->switch_cap = sw_cap;
4731                 ha->operating_mode = P2P;
4732                 ha->current_topology = ISP_CFG_F;
4733                 strcpy(connect_type, "(F_Port)");
4734                 break;
4735
4736         default:
4737                 ql_dbg(ql_dbg_disc, vha, 0x200f,
4738                     "HBA in unknown topology %x, using NL.\n", topo);
4739                 ha->switch_cap = 0;
4740                 ha->current_topology = ISP_CFG_NL;
4741                 strcpy(connect_type, "(Loop)");
4742                 break;
4743         }
4744
4745         /* Save Host port and loop ID. */
4746         /* byte order - Big Endian */
4747         id.b.domain = domain;
4748         id.b.area = area;
4749         id.b.al_pa = al_pa;
4750         id.b.rsvd_1 = 0;
4751         spin_lock_irqsave(&ha->hardware_lock, flags);
4752         if (vha->hw->flags.edif_enabled) {
4753                 if (topo != 2)
4754                         qlt_update_host_map(vha, id);
4755         } else if (!(topo == 2 && ha->flags.n2n_bigger))
4756                 qlt_update_host_map(vha, id);
4757         spin_unlock_irqrestore(&ha->hardware_lock, flags);
4758
4759         if (!vha->flags.init_done)
4760                 ql_log(ql_log_info, vha, 0x2010,
4761                     "Topology - %s, Host Loop address 0x%x.\n",
4762                     connect_type, vha->loop_id);
4763
4764         return(rval);
4765 }
4766
4767 inline void
4768 qla2x00_set_model_info(scsi_qla_host_t *vha, uint8_t *model, size_t len,
4769                        const char *def)
4770 {
4771         char *st, *en;
4772         uint16_t index;
4773         uint64_t zero[2] = { 0 };
4774         struct qla_hw_data *ha = vha->hw;
4775         int use_tbl = !IS_QLA24XX_TYPE(ha) && !IS_QLA25XX(ha) &&
4776             !IS_CNA_CAPABLE(ha) && !IS_QLA2031(ha);
4777
4778         if (len > sizeof(zero))
4779                 len = sizeof(zero);
4780         if (memcmp(model, &zero, len) != 0) {
4781                 memcpy(ha->model_number, model, len);
4782                 st = en = ha->model_number;
4783                 en += len - 1;
4784                 while (en > st) {
4785                         if (*en != 0x20 && *en != 0x00)
4786                                 break;
4787                         *en-- = '\0';
4788                 }
4789
4790                 index = (ha->pdev->subsystem_device & 0xff);
4791                 if (use_tbl &&
4792                     ha->pdev->subsystem_vendor == PCI_VENDOR_ID_QLOGIC &&
4793                     index < QLA_MODEL_NAMES)
4794                         strlcpy(ha->model_desc,
4795                             qla2x00_model_name[index * 2 + 1],
4796                             sizeof(ha->model_desc));
4797         } else {
4798                 index = (ha->pdev->subsystem_device & 0xff);
4799                 if (use_tbl &&
4800                     ha->pdev->subsystem_vendor == PCI_VENDOR_ID_QLOGIC &&
4801                     index < QLA_MODEL_NAMES) {
4802                         strlcpy(ha->model_number,
4803                                 qla2x00_model_name[index * 2],
4804                                 sizeof(ha->model_number));
4805                         strlcpy(ha->model_desc,
4806                             qla2x00_model_name[index * 2 + 1],
4807                             sizeof(ha->model_desc));
4808                 } else {
4809                         strlcpy(ha->model_number, def,
4810                                 sizeof(ha->model_number));
4811                 }
4812         }
4813         if (IS_FWI2_CAPABLE(ha))
4814                 qla2xxx_get_vpd_field(vha, "\x82", ha->model_desc,
4815                     sizeof(ha->model_desc));
4816 }
4817
4818 /* On sparc systems, obtain port and node WWN from firmware
4819  * properties.
4820  */
4821 static void qla2xxx_nvram_wwn_from_ofw(scsi_qla_host_t *vha, nvram_t *nv)
4822 {
4823 #ifdef CONFIG_SPARC
4824         struct qla_hw_data *ha = vha->hw;
4825         struct pci_dev *pdev = ha->pdev;
4826         struct device_node *dp = pci_device_to_OF_node(pdev);
4827         const u8 *val;
4828         int len;
4829
4830         val = of_get_property(dp, "port-wwn", &len);
4831         if (val && len >= WWN_SIZE)
4832                 memcpy(nv->port_name, val, WWN_SIZE);
4833
4834         val = of_get_property(dp, "node-wwn", &len);
4835         if (val && len >= WWN_SIZE)
4836                 memcpy(nv->node_name, val, WWN_SIZE);
4837 #endif
4838 }
4839
4840 /*
4841 * NVRAM configuration for ISP 2xxx
4842 *
4843 * Input:
4844 *      ha                = adapter block pointer.
4845 *
4846 * Output:
4847 *      initialization control block in response_ring
4848 *      host adapters parameters in host adapter block
4849 *
4850 * Returns:
4851 *      0 = success.
4852 */
4853 int
4854 qla2x00_nvram_config(scsi_qla_host_t *vha)
4855 {
4856         int             rval;
4857         uint8_t         chksum = 0;
4858         uint16_t        cnt;
4859         uint8_t         *dptr1, *dptr2;
4860         struct qla_hw_data *ha = vha->hw;
4861         init_cb_t       *icb = ha->init_cb;
4862         nvram_t         *nv = ha->nvram;
4863         uint8_t         *ptr = ha->nvram;
4864         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
4865
4866         rval = QLA_SUCCESS;
4867
4868         /* Determine NVRAM starting address. */
4869         ha->nvram_size = sizeof(*nv);
4870         ha->nvram_base = 0;
4871         if (!IS_QLA2100(ha) && !IS_QLA2200(ha) && !IS_QLA2300(ha))
4872                 if ((rd_reg_word(&reg->ctrl_status) >> 14) == 1)
4873                         ha->nvram_base = 0x80;
4874
4875         /* Get NVRAM data and calculate checksum. */
4876         ha->isp_ops->read_nvram(vha, ptr, ha->nvram_base, ha->nvram_size);
4877         for (cnt = 0, chksum = 0; cnt < ha->nvram_size; cnt++)
4878                 chksum += *ptr++;
4879
4880         ql_dbg(ql_dbg_init + ql_dbg_buffer, vha, 0x010f,
4881             "Contents of NVRAM.\n");
4882         ql_dump_buffer(ql_dbg_init + ql_dbg_buffer, vha, 0x0110,
4883             nv, ha->nvram_size);
4884
4885         /* Bad NVRAM data, set defaults parameters. */
4886         if (chksum || memcmp("ISP ", nv->id, sizeof(nv->id)) ||
4887             nv->nvram_version < 1) {
4888                 /* Reset NVRAM data. */
4889                 ql_log(ql_log_warn, vha, 0x0064,
4890                     "Inconsistent NVRAM detected: checksum=%#x id=%.4s version=%#x.\n",
4891                     chksum, nv->id, nv->nvram_version);
4892                 ql_log(ql_log_warn, vha, 0x0065,
4893                     "Falling back to "
4894                     "functioning (yet invalid -- WWPN) defaults.\n");
4895
4896                 /*
4897                  * Set default initialization control block.
4898                  */
4899                 memset(nv, 0, ha->nvram_size);
4900                 nv->parameter_block_version = ICB_VERSION;
4901
4902                 if (IS_QLA23XX(ha)) {
4903                         nv->firmware_options[0] = BIT_2 | BIT_1;
4904                         nv->firmware_options[1] = BIT_7 | BIT_5;
4905                         nv->add_firmware_options[0] = BIT_5;
4906                         nv->add_firmware_options[1] = BIT_5 | BIT_4;
4907                         nv->frame_payload_size = cpu_to_le16(2048);
4908                         nv->special_options[1] = BIT_7;
4909                 } else if (IS_QLA2200(ha)) {
4910                         nv->firmware_options[0] = BIT_2 | BIT_1;
4911                         nv->firmware_options[1] = BIT_7 | BIT_5;
4912                         nv->add_firmware_options[0] = BIT_5;
4913                         nv->add_firmware_options[1] = BIT_5 | BIT_4;
4914                         nv->frame_payload_size = cpu_to_le16(1024);
4915                 } else if (IS_QLA2100(ha)) {
4916                         nv->firmware_options[0] = BIT_3 | BIT_1;
4917                         nv->firmware_options[1] = BIT_5;
4918                         nv->frame_payload_size = cpu_to_le16(1024);
4919                 }
4920
4921                 nv->max_iocb_allocation = cpu_to_le16(256);
4922                 nv->execution_throttle = cpu_to_le16(16);
4923                 nv->retry_count = 8;
4924                 nv->retry_delay = 1;
4925
4926                 nv->port_name[0] = 33;
4927                 nv->port_name[3] = 224;
4928                 nv->port_name[4] = 139;
4929
4930                 qla2xxx_nvram_wwn_from_ofw(vha, nv);
4931
4932                 nv->login_timeout = 4;
4933
4934                 /*
4935                  * Set default host adapter parameters
4936                  */
4937                 nv->host_p[1] = BIT_2;
4938                 nv->reset_delay = 5;
4939                 nv->port_down_retry_count = 8;
4940                 nv->max_luns_per_target = cpu_to_le16(8);
4941                 nv->link_down_timeout = 60;
4942
4943                 rval = 1;
4944         }
4945
4946         /* Reset Initialization control block */
4947         memset(icb, 0, ha->init_cb_size);
4948
4949         /*
4950          * Setup driver NVRAM options.
4951          */
4952         nv->firmware_options[0] |= (BIT_6 | BIT_1);
4953         nv->firmware_options[0] &= ~(BIT_5 | BIT_4);
4954         nv->firmware_options[1] |= (BIT_5 | BIT_0);
4955         nv->firmware_options[1] &= ~BIT_4;
4956
4957         if (IS_QLA23XX(ha)) {
4958                 nv->firmware_options[0] |= BIT_2;
4959                 nv->firmware_options[0] &= ~BIT_3;
4960                 nv->special_options[0] &= ~BIT_6;
4961                 nv->add_firmware_options[1] |= BIT_5 | BIT_4;
4962
4963                 if (IS_QLA2300(ha)) {
4964                         if (ha->fb_rev == FPM_2310) {
4965                                 strcpy(ha->model_number, "QLA2310");
4966                         } else {
4967                                 strcpy(ha->model_number, "QLA2300");
4968                         }
4969                 } else {
4970                         qla2x00_set_model_info(vha, nv->model_number,
4971                             sizeof(nv->model_number), "QLA23xx");
4972                 }
4973         } else if (IS_QLA2200(ha)) {
4974                 nv->firmware_options[0] |= BIT_2;
4975                 /*
4976                  * 'Point-to-point preferred, else loop' is not a safe
4977                  * connection mode setting.
4978                  */
4979                 if ((nv->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) ==
4980                     (BIT_5 | BIT_4)) {
4981                         /* Force 'loop preferred, else point-to-point'. */
4982                         nv->add_firmware_options[0] &= ~(BIT_6 | BIT_5 | BIT_4);
4983                         nv->add_firmware_options[0] |= BIT_5;
4984                 }
4985                 strcpy(ha->model_number, "QLA22xx");
4986         } else /*if (IS_QLA2100(ha))*/ {
4987                 strcpy(ha->model_number, "QLA2100");
4988         }
4989
4990         /*
4991          * Copy over NVRAM RISC parameter block to initialization control block.
4992          */
4993         dptr1 = (uint8_t *)icb;
4994         dptr2 = (uint8_t *)&nv->parameter_block_version;
4995         cnt = (uint8_t *)&icb->request_q_outpointer - (uint8_t *)&icb->version;
4996         while (cnt--)
4997                 *dptr1++ = *dptr2++;
4998
4999         /* Copy 2nd half. */
5000         dptr1 = (uint8_t *)icb->add_firmware_options;
5001         cnt = (uint8_t *)icb->reserved_3 - (uint8_t *)icb->add_firmware_options;
5002         while (cnt--)
5003                 *dptr1++ = *dptr2++;
5004         ha->frame_payload_size = le16_to_cpu(icb->frame_payload_size);
5005         /* Use alternate WWN? */
5006         if (nv->host_p[1] & BIT_7) {
5007                 memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
5008                 memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
5009         }
5010
5011         /* Prepare nodename */
5012         if ((icb->firmware_options[1] & BIT_6) == 0) {
5013                 /*
5014                  * Firmware will apply the following mask if the nodename was
5015                  * not provided.
5016                  */
5017                 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
5018                 icb->node_name[0] &= 0xF0;
5019         }
5020
5021         /*
5022          * Set host adapter parameters.
5023          */
5024
5025         /*
5026          * BIT_7 in the host-parameters section allows for modification to
5027          * internal driver logging.
5028          */
5029         if (nv->host_p[0] & BIT_7)
5030                 ql2xextended_error_logging = QL_DBG_DEFAULT1_MASK;
5031         ha->flags.disable_risc_code_load = ((nv->host_p[0] & BIT_4) ? 1 : 0);
5032         /* Always load RISC code on non ISP2[12]00 chips. */
5033         if (!IS_QLA2100(ha) && !IS_QLA2200(ha))
5034                 ha->flags.disable_risc_code_load = 0;
5035         ha->flags.enable_lip_reset = ((nv->host_p[1] & BIT_1) ? 1 : 0);
5036         ha->flags.enable_lip_full_login = ((nv->host_p[1] & BIT_2) ? 1 : 0);
5037         ha->flags.enable_target_reset = ((nv->host_p[1] & BIT_3) ? 1 : 0);
5038         ha->flags.enable_led_scheme = (nv->special_options[1] & BIT_4) ? 1 : 0;
5039         ha->flags.disable_serdes = 0;
5040
5041         ha->operating_mode =
5042             (icb->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) >> 4;
5043
5044         memcpy(ha->fw_seriallink_options, nv->seriallink_options,
5045             sizeof(ha->fw_seriallink_options));
5046
5047         /* save HBA serial number */
5048         ha->serial0 = icb->port_name[5];
5049         ha->serial1 = icb->port_name[6];
5050         ha->serial2 = icb->port_name[7];
5051         memcpy(vha->node_name, icb->node_name, WWN_SIZE);
5052         memcpy(vha->port_name, icb->port_name, WWN_SIZE);
5053
5054         icb->execution_throttle = cpu_to_le16(0xFFFF);
5055
5056         ha->retry_count = nv->retry_count;
5057
5058         /* Set minimum login_timeout to 4 seconds. */
5059         if (nv->login_timeout != ql2xlogintimeout)
5060                 nv->login_timeout = ql2xlogintimeout;
5061         if (nv->login_timeout < 4)
5062                 nv->login_timeout = 4;
5063         ha->login_timeout = nv->login_timeout;
5064
5065         /* Set minimum RATOV to 100 tenths of a second. */
5066         ha->r_a_tov = 100;
5067
5068         ha->loop_reset_delay = nv->reset_delay;
5069
5070         /* Link Down Timeout = 0:
5071          *
5072          *      When Port Down timer expires we will start returning
5073          *      I/O's to OS with "DID_NO_CONNECT".
5074          *
5075          * Link Down Timeout != 0:
5076          *
5077          *       The driver waits for the link to come up after link down
5078          *       before returning I/Os to OS with "DID_NO_CONNECT".
5079          */
5080         if (nv->link_down_timeout == 0) {
5081                 ha->loop_down_abort_time =
5082                     (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
5083         } else {
5084                 ha->link_down_timeout =  nv->link_down_timeout;
5085                 ha->loop_down_abort_time =
5086                     (LOOP_DOWN_TIME - ha->link_down_timeout);
5087         }
5088
5089         /*
5090          * Need enough time to try and get the port back.
5091          */
5092         ha->port_down_retry_count = nv->port_down_retry_count;
5093         if (qlport_down_retry)
5094                 ha->port_down_retry_count = qlport_down_retry;
5095         /* Set login_retry_count */
5096         ha->login_retry_count  = nv->retry_count;
5097         if (ha->port_down_retry_count == nv->port_down_retry_count &&
5098             ha->port_down_retry_count > 3)
5099                 ha->login_retry_count = ha->port_down_retry_count;
5100         else if (ha->port_down_retry_count > (int)ha->login_retry_count)
5101                 ha->login_retry_count = ha->port_down_retry_count;
5102         if (ql2xloginretrycount)
5103                 ha->login_retry_count = ql2xloginretrycount;
5104
5105         icb->lun_enables = cpu_to_le16(0);
5106         icb->command_resource_count = 0;
5107         icb->immediate_notify_resource_count = 0;
5108         icb->timeout = cpu_to_le16(0);
5109
5110         if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
5111                 /* Enable RIO */
5112                 icb->firmware_options[0] &= ~BIT_3;
5113                 icb->add_firmware_options[0] &=
5114                     ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
5115                 icb->add_firmware_options[0] |= BIT_2;
5116                 icb->response_accumulation_timer = 3;
5117                 icb->interrupt_delay_timer = 5;
5118
5119                 vha->flags.process_response_queue = 1;
5120         } else {
5121                 /* Enable ZIO. */
5122                 if (!vha->flags.init_done) {
5123                         ha->zio_mode = icb->add_firmware_options[0] &
5124                             (BIT_3 | BIT_2 | BIT_1 | BIT_0);
5125                         ha->zio_timer = icb->interrupt_delay_timer ?
5126                             icb->interrupt_delay_timer : 2;
5127                 }
5128                 icb->add_firmware_options[0] &=
5129                     ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
5130                 vha->flags.process_response_queue = 0;
5131                 if (ha->zio_mode != QLA_ZIO_DISABLED) {
5132                         ha->zio_mode = QLA_ZIO_MODE_6;
5133
5134                         ql_log(ql_log_info, vha, 0x0068,
5135                             "ZIO mode %d enabled; timer delay (%d us).\n",
5136                             ha->zio_mode, ha->zio_timer * 100);
5137
5138                         icb->add_firmware_options[0] |= (uint8_t)ha->zio_mode;
5139                         icb->interrupt_delay_timer = (uint8_t)ha->zio_timer;
5140                         vha->flags.process_response_queue = 1;
5141                 }
5142         }
5143
5144         if (rval) {
5145                 ql_log(ql_log_warn, vha, 0x0069,
5146                     "NVRAM configuration failed.\n");
5147         }
5148         return (rval);
5149 }
5150
5151 static void
5152 qla2x00_rport_del(void *data)
5153 {
5154         fc_port_t *fcport = data;
5155         struct fc_rport *rport;
5156         unsigned long flags;
5157
5158         spin_lock_irqsave(fcport->vha->host->host_lock, flags);
5159         rport = fcport->drport ? fcport->drport : fcport->rport;
5160         fcport->drport = NULL;
5161         spin_unlock_irqrestore(fcport->vha->host->host_lock, flags);
5162         if (rport) {
5163                 ql_dbg(ql_dbg_disc, fcport->vha, 0x210b,
5164                     "%s %8phN. rport %p roles %x\n",
5165                     __func__, fcport->port_name, rport,
5166                     rport->roles);
5167
5168                 fc_remote_port_delete(rport);
5169         }
5170 }
5171
5172 void qla2x00_set_fcport_state(fc_port_t *fcport, int state)
5173 {
5174         int old_state;
5175
5176         old_state = atomic_read(&fcport->state);
5177         atomic_set(&fcport->state, state);
5178
5179         /* Don't print state transitions during initial allocation of fcport */
5180         if (old_state && old_state != state) {
5181                 ql_dbg(ql_dbg_disc, fcport->vha, 0x207d,
5182                        "FCPort %8phC state transitioned from %s to %s - portid=%02x%02x%02x.\n",
5183                        fcport->port_name, port_state_str[old_state],
5184                        port_state_str[state], fcport->d_id.b.domain,
5185                        fcport->d_id.b.area, fcport->d_id.b.al_pa);
5186         }
5187 }
5188
5189 /**
5190  * qla2x00_alloc_fcport() - Allocate a generic fcport.
5191  * @vha: HA context
5192  * @flags: allocation flags
5193  *
5194  * Returns a pointer to the allocated fcport, or NULL, if none available.
5195  */
5196 fc_port_t *
5197 qla2x00_alloc_fcport(scsi_qla_host_t *vha, gfp_t flags)
5198 {
5199         fc_port_t *fcport;
5200
5201         fcport = kzalloc(sizeof(fc_port_t), flags);
5202         if (!fcport)
5203                 return NULL;
5204
5205         fcport->ct_desc.ct_sns = dma_alloc_coherent(&vha->hw->pdev->dev,
5206                 sizeof(struct ct_sns_pkt), &fcport->ct_desc.ct_sns_dma,
5207                 flags);
5208         if (!fcport->ct_desc.ct_sns) {
5209                 ql_log(ql_log_warn, vha, 0xd049,
5210                     "Failed to allocate ct_sns request.\n");
5211                 kfree(fcport);
5212                 return NULL;
5213         }
5214
5215         /* Setup fcport template structure. */
5216         fcport->vha = vha;
5217         fcport->port_type = FCT_UNKNOWN;
5218         fcport->loop_id = FC_NO_LOOP_ID;
5219         qla2x00_set_fcport_state(fcport, FCS_UNCONFIGURED);
5220         fcport->supported_classes = FC_COS_UNSPECIFIED;
5221         fcport->fp_speed = PORT_SPEED_UNKNOWN;
5222
5223         fcport->disc_state = DSC_DELETED;
5224         fcport->fw_login_state = DSC_LS_PORT_UNAVAIL;
5225         fcport->deleted = QLA_SESS_DELETED;
5226         fcport->login_retry = vha->hw->login_retry_count;
5227         fcport->chip_reset = vha->hw->base_qpair->chip_reset;
5228         fcport->logout_on_delete = 1;
5229         fcport->tgt_link_down_time = QLA2XX_MAX_LINK_DOWN_TIME;
5230         fcport->tgt_short_link_down_cnt = 0;
5231         fcport->dev_loss_tmo = 0;
5232
5233         if (!fcport->ct_desc.ct_sns) {
5234                 ql_log(ql_log_warn, vha, 0xd049,
5235                     "Failed to allocate ct_sns request.\n");
5236                 kfree(fcport);
5237                 return NULL;
5238         }
5239
5240         INIT_WORK(&fcport->del_work, qla24xx_delete_sess_fn);
5241         INIT_WORK(&fcport->free_work, qlt_free_session_done);
5242         INIT_WORK(&fcport->reg_work, qla_register_fcport_fn);
5243         INIT_LIST_HEAD(&fcport->gnl_entry);
5244         INIT_LIST_HEAD(&fcport->list);
5245
5246         INIT_LIST_HEAD(&fcport->sess_cmd_list);
5247         spin_lock_init(&fcport->sess_cmd_lock);
5248
5249         spin_lock_init(&fcport->edif.sa_list_lock);
5250         INIT_LIST_HEAD(&fcport->edif.tx_sa_list);
5251         INIT_LIST_HEAD(&fcport->edif.rx_sa_list);
5252
5253         if (vha->e_dbell.db_flags == EDB_ACTIVE)
5254                 fcport->edif.app_started = 1;
5255
5256         spin_lock_init(&fcport->edif.indx_list_lock);
5257         INIT_LIST_HEAD(&fcport->edif.edif_indx_list);
5258
5259         return fcport;
5260 }
5261
5262 void
5263 qla2x00_free_fcport(fc_port_t *fcport)
5264 {
5265         if (fcport->ct_desc.ct_sns) {
5266                 dma_free_coherent(&fcport->vha->hw->pdev->dev,
5267                         sizeof(struct ct_sns_pkt), fcport->ct_desc.ct_sns,
5268                         fcport->ct_desc.ct_sns_dma);
5269
5270                 fcport->ct_desc.ct_sns = NULL;
5271         }
5272
5273         qla_edif_flush_sa_ctl_lists(fcport);
5274         list_del(&fcport->list);
5275         qla2x00_clear_loop_id(fcport);
5276
5277         qla_edif_list_del(fcport);
5278
5279         kfree(fcport);
5280 }
5281
5282 static void qla_get_login_template(scsi_qla_host_t *vha)
5283 {
5284         struct qla_hw_data *ha = vha->hw;
5285         int rval;
5286         u32 *bp, sz;
5287         __be32 *q;
5288
5289         memset(ha->init_cb, 0, ha->init_cb_size);
5290         sz = min_t(int, sizeof(struct fc_els_flogi), ha->init_cb_size);
5291         rval = qla24xx_get_port_login_templ(vha, ha->init_cb_dma,
5292                                             ha->init_cb, sz);
5293         if (rval != QLA_SUCCESS) {
5294                 ql_dbg(ql_dbg_init, vha, 0x00d1,
5295                        "PLOGI ELS param read fail.\n");
5296                 return;
5297         }
5298         q = (__be32 *)&ha->plogi_els_payld.fl_csp;
5299
5300         bp = (uint32_t *)ha->init_cb;
5301         cpu_to_be32_array(q, bp, sz / 4);
5302         ha->flags.plogi_template_valid = 1;
5303 }
5304
5305 /*
5306  * qla2x00_configure_loop
5307  *      Updates Fibre Channel Device Database with what is actually on loop.
5308  *
5309  * Input:
5310  *      ha                = adapter block pointer.
5311  *
5312  * Returns:
5313  *      0 = success.
5314  *      1 = error.
5315  *      2 = database was full and device was not configured.
5316  */
5317 static int
5318 qla2x00_configure_loop(scsi_qla_host_t *vha)
5319 {
5320         int  rval;
5321         unsigned long flags, save_flags;
5322         struct qla_hw_data *ha = vha->hw;
5323
5324         rval = QLA_SUCCESS;
5325
5326         /* Get Initiator ID */
5327         if (test_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags)) {
5328                 rval = qla2x00_configure_hba(vha);
5329                 if (rval != QLA_SUCCESS) {
5330                         ql_dbg(ql_dbg_disc, vha, 0x2013,
5331                             "Unable to configure HBA.\n");
5332                         return (rval);
5333                 }
5334         }
5335
5336         save_flags = flags = vha->dpc_flags;
5337         ql_dbg(ql_dbg_disc, vha, 0x2014,
5338             "Configure loop -- dpc flags = 0x%lx.\n", flags);
5339
5340         /*
5341          * If we have both an RSCN and PORT UPDATE pending then handle them
5342          * both at the same time.
5343          */
5344         clear_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
5345         clear_bit(RSCN_UPDATE, &vha->dpc_flags);
5346
5347         qla2x00_get_data_rate(vha);
5348         qla_get_login_template(vha);
5349
5350         /* Determine what we need to do */
5351         if ((ha->current_topology == ISP_CFG_FL ||
5352             ha->current_topology == ISP_CFG_F) &&
5353             (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
5354
5355                 set_bit(RSCN_UPDATE, &flags);
5356                 clear_bit(LOCAL_LOOP_UPDATE, &flags);
5357
5358         } else if (ha->current_topology == ISP_CFG_NL ||
5359                    ha->current_topology == ISP_CFG_N) {
5360                 clear_bit(RSCN_UPDATE, &flags);
5361                 set_bit(LOCAL_LOOP_UPDATE, &flags);
5362         } else if (!vha->flags.online ||
5363             (test_bit(ABORT_ISP_ACTIVE, &flags))) {
5364                 set_bit(RSCN_UPDATE, &flags);
5365                 set_bit(LOCAL_LOOP_UPDATE, &flags);
5366         }
5367
5368         if (test_bit(LOCAL_LOOP_UPDATE, &flags)) {
5369                 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)) {
5370                         ql_dbg(ql_dbg_disc, vha, 0x2015,
5371                             "Loop resync needed, failing.\n");
5372                         rval = QLA_FUNCTION_FAILED;
5373                 } else
5374                         rval = qla2x00_configure_local_loop(vha);
5375         }
5376
5377         if (rval == QLA_SUCCESS && test_bit(RSCN_UPDATE, &flags)) {
5378                 if (LOOP_TRANSITION(vha)) {
5379                         ql_dbg(ql_dbg_disc, vha, 0x2099,
5380                             "Needs RSCN update and loop transition.\n");
5381                         rval = QLA_FUNCTION_FAILED;
5382                 }
5383                 else
5384                         rval = qla2x00_configure_fabric(vha);
5385         }
5386
5387         if (rval == QLA_SUCCESS) {
5388                 if (atomic_read(&vha->loop_down_timer) ||
5389                     test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)) {
5390                         rval = QLA_FUNCTION_FAILED;
5391                 } else {
5392                         atomic_set(&vha->loop_state, LOOP_READY);
5393                         ql_dbg(ql_dbg_disc, vha, 0x2069,
5394                             "LOOP READY.\n");
5395                         ha->flags.fw_init_done = 1;
5396
5397                         /*
5398                          * use link up to wake up app to get ready for
5399                          * authentication.
5400                          */
5401                         if (ha->flags.edif_enabled && DBELL_INACTIVE(vha))
5402                                 qla2x00_post_aen_work(vha, FCH_EVT_LINKUP,
5403                                                       ha->link_data_rate);
5404
5405                         /*
5406                          * Process any ATIO queue entries that came in
5407                          * while we weren't online.
5408                          */
5409                         if (qla_tgt_mode_enabled(vha) ||
5410                             qla_dual_mode_enabled(vha)) {
5411                                 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
5412                                 qlt_24xx_process_atio_queue(vha, 0);
5413                                 spin_unlock_irqrestore(&ha->tgt.atio_lock,
5414                                     flags);
5415                         }
5416                 }
5417         }
5418
5419         if (rval) {
5420                 ql_dbg(ql_dbg_disc, vha, 0x206a,
5421                     "%s *** FAILED ***.\n", __func__);
5422         } else {
5423                 ql_dbg(ql_dbg_disc, vha, 0x206b,
5424                     "%s: exiting normally. local port wwpn %8phN id %06x)\n",
5425                     __func__, vha->port_name, vha->d_id.b24);
5426         }
5427
5428         /* Restore state if a resync event occurred during processing */
5429         if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)) {
5430                 if (test_bit(LOCAL_LOOP_UPDATE, &save_flags))
5431                         set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
5432                 if (test_bit(RSCN_UPDATE, &save_flags)) {
5433                         set_bit(RSCN_UPDATE, &vha->dpc_flags);
5434                 }
5435         }
5436
5437         return (rval);
5438 }
5439
5440 static int qla2x00_configure_n2n_loop(scsi_qla_host_t *vha)
5441 {
5442         unsigned long flags;
5443         fc_port_t *fcport;
5444
5445         ql_dbg(ql_dbg_disc, vha, 0x206a, "%s %d.\n", __func__, __LINE__);
5446
5447         if (test_and_clear_bit(N2N_LOGIN_NEEDED, &vha->dpc_flags))
5448                 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
5449
5450         list_for_each_entry(fcport, &vha->vp_fcports, list) {
5451                 if (fcport->n2n_flag) {
5452                         qla24xx_fcport_handle_login(vha, fcport);
5453                         return QLA_SUCCESS;
5454                 }
5455         }
5456
5457         spin_lock_irqsave(&vha->work_lock, flags);
5458         vha->scan.scan_retry++;
5459         spin_unlock_irqrestore(&vha->work_lock, flags);
5460
5461         if (vha->scan.scan_retry < MAX_SCAN_RETRIES) {
5462                 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
5463                 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
5464         }
5465         return QLA_FUNCTION_FAILED;
5466 }
5467
5468 /*
5469  * qla2x00_configure_local_loop
5470  *      Updates Fibre Channel Device Database with local loop devices.
5471  *
5472  * Input:
5473  *      ha = adapter block pointer.
5474  *
5475  * Returns:
5476  *      0 = success.
5477  */
5478 static int
5479 qla2x00_configure_local_loop(scsi_qla_host_t *vha)
5480 {
5481         int             rval, rval2;
5482         int             found_devs;
5483         int             found;
5484         fc_port_t       *fcport, *new_fcport;
5485         uint16_t        index;
5486         uint16_t        entries;
5487         struct gid_list_info *gid;
5488         uint16_t        loop_id;
5489         uint8_t         domain, area, al_pa;
5490         struct qla_hw_data *ha = vha->hw;
5491         unsigned long flags;
5492
5493         /* Inititae N2N login. */
5494         if (N2N_TOPO(ha))
5495                 return qla2x00_configure_n2n_loop(vha);
5496
5497         found_devs = 0;
5498         new_fcport = NULL;
5499         entries = MAX_FIBRE_DEVICES_LOOP;
5500
5501         /* Get list of logged in devices. */
5502         memset(ha->gid_list, 0, qla2x00_gid_list_size(ha));
5503         rval = qla2x00_get_id_list(vha, ha->gid_list, ha->gid_list_dma,
5504             &entries);
5505         if (rval != QLA_SUCCESS)
5506                 goto err;
5507
5508         ql_dbg(ql_dbg_disc, vha, 0x2011,
5509             "Entries in ID list (%d).\n", entries);
5510         ql_dump_buffer(ql_dbg_disc + ql_dbg_buffer, vha, 0x2075,
5511             ha->gid_list, entries * sizeof(*ha->gid_list));
5512
5513         if (entries == 0) {
5514                 spin_lock_irqsave(&vha->work_lock, flags);
5515                 vha->scan.scan_retry++;
5516                 spin_unlock_irqrestore(&vha->work_lock, flags);
5517
5518                 if (vha->scan.scan_retry < MAX_SCAN_RETRIES) {
5519                         set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
5520                         set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
5521                 }
5522         } else {
5523                 vha->scan.scan_retry = 0;
5524         }
5525
5526         list_for_each_entry(fcport, &vha->vp_fcports, list) {
5527                 fcport->scan_state = QLA_FCPORT_SCAN;
5528         }
5529
5530         /* Allocate temporary fcport for any new fcports discovered. */
5531         new_fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
5532         if (new_fcport == NULL) {
5533                 ql_log(ql_log_warn, vha, 0x2012,
5534                     "Memory allocation failed for fcport.\n");
5535                 rval = QLA_MEMORY_ALLOC_FAILED;
5536                 goto err;
5537         }
5538         new_fcport->flags &= ~FCF_FABRIC_DEVICE;
5539
5540         /* Add devices to port list. */
5541         gid = ha->gid_list;
5542         for (index = 0; index < entries; index++) {
5543                 domain = gid->domain;
5544                 area = gid->area;
5545                 al_pa = gid->al_pa;
5546                 if (IS_QLA2100(ha) || IS_QLA2200(ha))
5547                         loop_id = gid->loop_id_2100;
5548                 else
5549                         loop_id = le16_to_cpu(gid->loop_id);
5550                 gid = (void *)gid + ha->gid_list_info_size;
5551
5552                 /* Bypass reserved domain fields. */
5553                 if ((domain & 0xf0) == 0xf0)
5554                         continue;
5555
5556                 /* Bypass if not same domain and area of adapter. */
5557                 if (area && domain && ((area != vha->d_id.b.area) ||
5558                     (domain != vha->d_id.b.domain)) &&
5559                     (ha->current_topology == ISP_CFG_NL))
5560                         continue;
5561
5562
5563                 /* Bypass invalid local loop ID. */
5564                 if (loop_id > LAST_LOCAL_LOOP_ID)
5565                         continue;
5566
5567                 memset(new_fcport->port_name, 0, WWN_SIZE);
5568
5569                 /* Fill in member data. */
5570                 new_fcport->d_id.b.domain = domain;
5571                 new_fcport->d_id.b.area = area;
5572                 new_fcport->d_id.b.al_pa = al_pa;
5573                 new_fcport->loop_id = loop_id;
5574                 new_fcport->scan_state = QLA_FCPORT_FOUND;
5575
5576                 rval2 = qla2x00_get_port_database(vha, new_fcport, 0);
5577                 if (rval2 != QLA_SUCCESS) {
5578                         ql_dbg(ql_dbg_disc, vha, 0x2097,
5579                             "Failed to retrieve fcport information "
5580                             "-- get_port_database=%x, loop_id=0x%04x.\n",
5581                             rval2, new_fcport->loop_id);
5582                         /* Skip retry if N2N */
5583                         if (ha->current_topology != ISP_CFG_N) {
5584                                 ql_dbg(ql_dbg_disc, vha, 0x2105,
5585                                     "Scheduling resync.\n");
5586                                 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
5587                                 continue;
5588                         }
5589                 }
5590
5591                 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
5592                 /* Check for matching device in port list. */
5593                 found = 0;
5594                 fcport = NULL;
5595                 list_for_each_entry(fcport, &vha->vp_fcports, list) {
5596                         if (memcmp(new_fcport->port_name, fcport->port_name,
5597                             WWN_SIZE))
5598                                 continue;
5599
5600                         fcport->flags &= ~FCF_FABRIC_DEVICE;
5601                         fcport->loop_id = new_fcport->loop_id;
5602                         fcport->port_type = new_fcport->port_type;
5603                         fcport->d_id.b24 = new_fcport->d_id.b24;
5604                         memcpy(fcport->node_name, new_fcport->node_name,
5605                             WWN_SIZE);
5606                         fcport->scan_state = QLA_FCPORT_FOUND;
5607                         found++;
5608                         break;
5609                 }
5610
5611                 if (!found) {
5612                         /* New device, add to fcports list. */
5613                         list_add_tail(&new_fcport->list, &vha->vp_fcports);
5614
5615                         /* Allocate a new replacement fcport. */
5616                         fcport = new_fcport;
5617
5618                         spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
5619
5620                         new_fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
5621
5622                         if (new_fcport == NULL) {
5623                                 ql_log(ql_log_warn, vha, 0xd031,
5624                                     "Failed to allocate memory for fcport.\n");
5625                                 rval = QLA_MEMORY_ALLOC_FAILED;
5626                                 goto err;
5627                         }
5628                         spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
5629                         new_fcport->flags &= ~FCF_FABRIC_DEVICE;
5630                 }
5631
5632                 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
5633
5634                 /* Base iIDMA settings on HBA port speed. */
5635                 fcport->fp_speed = ha->link_data_rate;
5636
5637                 found_devs++;
5638         }
5639
5640         list_for_each_entry(fcport, &vha->vp_fcports, list) {
5641                 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5642                         break;
5643
5644                 if (fcport->scan_state == QLA_FCPORT_SCAN) {
5645                         if ((qla_dual_mode_enabled(vha) ||
5646                             qla_ini_mode_enabled(vha)) &&
5647                             atomic_read(&fcport->state) == FCS_ONLINE) {
5648                                 qla2x00_mark_device_lost(vha, fcport,
5649                                         ql2xplogiabsentdevice);
5650                                 if (fcport->loop_id != FC_NO_LOOP_ID &&
5651                                     (fcport->flags & FCF_FCP2_DEVICE) == 0 &&
5652                                     fcport->port_type != FCT_INITIATOR &&
5653                                     fcport->port_type != FCT_BROADCAST) {
5654                                         ql_dbg(ql_dbg_disc, vha, 0x20f0,
5655                                             "%s %d %8phC post del sess\n",
5656                                             __func__, __LINE__,
5657                                             fcport->port_name);
5658
5659                                         qlt_schedule_sess_for_deletion(fcport);
5660                                         continue;
5661                                 }
5662                         }
5663                 }
5664
5665                 if (fcport->scan_state == QLA_FCPORT_FOUND)
5666                         qla24xx_fcport_handle_login(vha, fcport);
5667         }
5668
5669         qla2x00_free_fcport(new_fcport);
5670
5671         return rval;
5672
5673 err:
5674         ql_dbg(ql_dbg_disc, vha, 0x2098,
5675                "Configure local loop error exit: rval=%x.\n", rval);
5676         return rval;
5677 }
5678
5679 static void
5680 qla2x00_iidma_fcport(scsi_qla_host_t *vha, fc_port_t *fcport)
5681 {
5682         int rval;
5683         uint16_t mb[MAILBOX_REGISTER_COUNT];
5684         struct qla_hw_data *ha = vha->hw;
5685
5686         if (!IS_IIDMA_CAPABLE(ha))
5687                 return;
5688
5689         if (atomic_read(&fcport->state) != FCS_ONLINE)
5690                 return;
5691
5692         if (fcport->fp_speed == PORT_SPEED_UNKNOWN ||
5693             fcport->fp_speed > ha->link_data_rate ||
5694             !ha->flags.gpsc_supported)
5695                 return;
5696
5697         rval = qla2x00_set_idma_speed(vha, fcport->loop_id, fcport->fp_speed,
5698             mb);
5699         if (rval != QLA_SUCCESS) {
5700                 ql_dbg(ql_dbg_disc, vha, 0x2004,
5701                     "Unable to adjust iIDMA %8phN -- %04x %x %04x %04x.\n",
5702                     fcport->port_name, rval, fcport->fp_speed, mb[0], mb[1]);
5703         } else {
5704                 ql_dbg(ql_dbg_disc, vha, 0x2005,
5705                     "iIDMA adjusted to %s GB/s (%X) on %8phN.\n",
5706                     qla2x00_get_link_speed_str(ha, fcport->fp_speed),
5707                     fcport->fp_speed, fcport->port_name);
5708         }
5709 }
5710
5711 void qla_do_iidma_work(struct scsi_qla_host *vha, fc_port_t *fcport)
5712 {
5713         qla2x00_iidma_fcport(vha, fcport);
5714         qla24xx_update_fcport_fcp_prio(vha, fcport);
5715 }
5716
5717 int qla_post_iidma_work(struct scsi_qla_host *vha, fc_port_t *fcport)
5718 {
5719         struct qla_work_evt *e;
5720
5721         e = qla2x00_alloc_work(vha, QLA_EVT_IIDMA);
5722         if (!e)
5723                 return QLA_FUNCTION_FAILED;
5724
5725         e->u.fcport.fcport = fcport;
5726         return qla2x00_post_work(vha, e);
5727 }
5728
5729 /* qla2x00_reg_remote_port is reserved for Initiator Mode only.*/
5730 static void
5731 qla2x00_reg_remote_port(scsi_qla_host_t *vha, fc_port_t *fcport)
5732 {
5733         struct fc_rport_identifiers rport_ids;
5734         struct fc_rport *rport;
5735         unsigned long flags;
5736
5737         if (atomic_read(&fcport->state) == FCS_ONLINE)
5738                 return;
5739
5740         rport_ids.node_name = wwn_to_u64(fcport->node_name);
5741         rport_ids.port_name = wwn_to_u64(fcport->port_name);
5742         rport_ids.port_id = fcport->d_id.b.domain << 16 |
5743             fcport->d_id.b.area << 8 | fcport->d_id.b.al_pa;
5744         rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
5745         fcport->rport = rport = fc_remote_port_add(vha->host, 0, &rport_ids);
5746         if (!rport) {
5747                 ql_log(ql_log_warn, vha, 0x2006,
5748                     "Unable to allocate fc remote port.\n");
5749                 return;
5750         }
5751
5752         spin_lock_irqsave(fcport->vha->host->host_lock, flags);
5753         *((fc_port_t **)rport->dd_data) = fcport;
5754         spin_unlock_irqrestore(fcport->vha->host->host_lock, flags);
5755         fcport->dev_loss_tmo = rport->dev_loss_tmo;
5756
5757         rport->supported_classes = fcport->supported_classes;
5758
5759         rport_ids.roles = FC_PORT_ROLE_UNKNOWN;
5760         if (fcport->port_type == FCT_INITIATOR)
5761                 rport_ids.roles |= FC_PORT_ROLE_FCP_INITIATOR;
5762         if (fcport->port_type == FCT_TARGET)
5763                 rport_ids.roles |= FC_PORT_ROLE_FCP_TARGET;
5764         if (fcport->port_type & FCT_NVME_INITIATOR)
5765                 rport_ids.roles |= FC_PORT_ROLE_NVME_INITIATOR;
5766         if (fcport->port_type & FCT_NVME_TARGET)
5767                 rport_ids.roles |= FC_PORT_ROLE_NVME_TARGET;
5768         if (fcport->port_type & FCT_NVME_DISCOVERY)
5769                 rport_ids.roles |= FC_PORT_ROLE_NVME_DISCOVERY;
5770
5771         fc_remote_port_rolechg(rport, rport_ids.roles);
5772
5773         ql_dbg(ql_dbg_disc, vha, 0x20ee,
5774             "%s: %8phN. rport %ld:0:%d (%p) is %s mode\n",
5775             __func__, fcport->port_name, vha->host_no,
5776             rport->scsi_target_id, rport,
5777             (fcport->port_type == FCT_TARGET) ? "tgt" :
5778             ((fcport->port_type & FCT_NVME) ? "nvme" : "ini"));
5779 }
5780
5781 /*
5782  * qla2x00_update_fcport
5783  *      Updates device on list.
5784  *
5785  * Input:
5786  *      ha = adapter block pointer.
5787  *      fcport = port structure pointer.
5788  *
5789  * Return:
5790  *      0  - Success
5791  *  BIT_0 - error
5792  *
5793  * Context:
5794  *      Kernel context.
5795  */
5796 void
5797 qla2x00_update_fcport(scsi_qla_host_t *vha, fc_port_t *fcport)
5798 {
5799         if (IS_SW_RESV_ADDR(fcport->d_id))
5800                 return;
5801
5802         ql_dbg(ql_dbg_disc, vha, 0x20ef, "%s %8phC\n",
5803             __func__, fcport->port_name);
5804
5805         qla2x00_set_fcport_disc_state(fcport, DSC_UPD_FCPORT);
5806         fcport->login_retry = vha->hw->login_retry_count;
5807         fcport->flags &= ~(FCF_LOGIN_NEEDED | FCF_ASYNC_SENT);
5808         fcport->deleted = 0;
5809         if (vha->hw->current_topology == ISP_CFG_NL)
5810                 fcport->logout_on_delete = 0;
5811         else
5812                 fcport->logout_on_delete = 1;
5813         fcport->n2n_chip_reset = fcport->n2n_link_reset_cnt = 0;
5814
5815         if (fcport->tgt_link_down_time < fcport->dev_loss_tmo) {
5816                 fcport->tgt_short_link_down_cnt++;
5817                 fcport->tgt_link_down_time = QLA2XX_MAX_LINK_DOWN_TIME;
5818         }
5819
5820         switch (vha->hw->current_topology) {
5821         case ISP_CFG_N:
5822         case ISP_CFG_NL:
5823                 fcport->keep_nport_handle = 1;
5824                 break;
5825         default:
5826                 break;
5827         }
5828
5829         qla2x00_iidma_fcport(vha, fcport);
5830
5831         qla2x00_dfs_create_rport(vha, fcport);
5832
5833         qla24xx_update_fcport_fcp_prio(vha, fcport);
5834
5835         switch (vha->host->active_mode) {
5836         case MODE_INITIATOR:
5837                 qla2x00_reg_remote_port(vha, fcport);
5838                 break;
5839         case MODE_TARGET:
5840                 if (!vha->vha_tgt.qla_tgt->tgt_stop &&
5841                         !vha->vha_tgt.qla_tgt->tgt_stopped)
5842                         qlt_fc_port_added(vha, fcport);
5843                 break;
5844         case MODE_DUAL:
5845                 qla2x00_reg_remote_port(vha, fcport);
5846                 if (!vha->vha_tgt.qla_tgt->tgt_stop &&
5847                         !vha->vha_tgt.qla_tgt->tgt_stopped)
5848                         qlt_fc_port_added(vha, fcport);
5849                 break;
5850         default:
5851                 break;
5852         }
5853
5854         if (NVME_TARGET(vha->hw, fcport))
5855                 qla_nvme_register_remote(vha, fcport);
5856
5857         qla2x00_set_fcport_state(fcport, FCS_ONLINE);
5858
5859         if (IS_IIDMA_CAPABLE(vha->hw) && vha->hw->flags.gpsc_supported) {
5860                 if (fcport->id_changed) {
5861                         fcport->id_changed = 0;
5862                         ql_dbg(ql_dbg_disc, vha, 0x20d7,
5863                             "%s %d %8phC post gfpnid fcp_cnt %d\n",
5864                             __func__, __LINE__, fcport->port_name,
5865                             vha->fcport_count);
5866                         qla24xx_post_gfpnid_work(vha, fcport);
5867                 } else {
5868                         ql_dbg(ql_dbg_disc, vha, 0x20d7,
5869                             "%s %d %8phC post gpsc fcp_cnt %d\n",
5870                             __func__, __LINE__, fcport->port_name,
5871                             vha->fcport_count);
5872                         qla24xx_post_gpsc_work(vha, fcport);
5873                 }
5874         }
5875
5876         qla2x00_set_fcport_disc_state(fcport, DSC_LOGIN_COMPLETE);
5877 }
5878
5879 void qla_register_fcport_fn(struct work_struct *work)
5880 {
5881         fc_port_t *fcport = container_of(work, struct fc_port, reg_work);
5882         u32 rscn_gen = fcport->rscn_gen;
5883         u16 data[2];
5884
5885         if (IS_SW_RESV_ADDR(fcport->d_id))
5886                 return;
5887
5888         qla2x00_update_fcport(fcport->vha, fcport);
5889
5890         ql_dbg(ql_dbg_disc, fcport->vha, 0x911e,
5891                "%s rscn gen %d/%d next DS %d\n", __func__,
5892                rscn_gen, fcport->rscn_gen, fcport->next_disc_state);
5893
5894         if (rscn_gen != fcport->rscn_gen) {
5895                 /* RSCN(s) came in while registration */
5896                 switch (fcport->next_disc_state) {
5897                 case DSC_DELETE_PEND:
5898                         qlt_schedule_sess_for_deletion(fcport);
5899                         break;
5900                 case DSC_ADISC:
5901                         data[0] = data[1] = 0;
5902                         qla2x00_post_async_adisc_work(fcport->vha, fcport,
5903                             data);
5904                         break;
5905                 default:
5906                         break;
5907                 }
5908         }
5909 }
5910
5911 /*
5912  * qla2x00_configure_fabric
5913  *      Setup SNS devices with loop ID's.
5914  *
5915  * Input:
5916  *      ha = adapter block pointer.
5917  *
5918  * Returns:
5919  *      0 = success.
5920  *      BIT_0 = error
5921  */
5922 static int
5923 qla2x00_configure_fabric(scsi_qla_host_t *vha)
5924 {
5925         int     rval;
5926         fc_port_t       *fcport;
5927         uint16_t        mb[MAILBOX_REGISTER_COUNT];
5928         uint16_t        loop_id;
5929         LIST_HEAD(new_fcports);
5930         struct qla_hw_data *ha = vha->hw;
5931         int             discovery_gen;
5932
5933         /* If FL port exists, then SNS is present */
5934         if (IS_FWI2_CAPABLE(ha))
5935                 loop_id = NPH_F_PORT;
5936         else
5937                 loop_id = SNS_FL_PORT;
5938         rval = qla2x00_get_port_name(vha, loop_id, vha->fabric_node_name, 1);
5939         if (rval != QLA_SUCCESS) {
5940                 ql_dbg(ql_dbg_disc, vha, 0x20a0,
5941                     "MBX_GET_PORT_NAME failed, No FL Port.\n");
5942
5943                 vha->device_flags &= ~SWITCH_FOUND;
5944                 return (QLA_SUCCESS);
5945         }
5946         vha->device_flags |= SWITCH_FOUND;
5947
5948         rval = qla2x00_get_port_name(vha, loop_id, vha->fabric_port_name, 0);
5949         if (rval != QLA_SUCCESS)
5950                 ql_dbg(ql_dbg_disc, vha, 0x20ff,
5951                     "Failed to get Fabric Port Name\n");
5952
5953         if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
5954                 rval = qla2x00_send_change_request(vha, 0x3, 0);
5955                 if (rval != QLA_SUCCESS)
5956                         ql_log(ql_log_warn, vha, 0x121,
5957                             "Failed to enable receiving of RSCN requests: 0x%x.\n",
5958                             rval);
5959         }
5960
5961         do {
5962                 qla2x00_mgmt_svr_login(vha);
5963
5964                 /* Ensure we are logged into the SNS. */
5965                 loop_id = NPH_SNS_LID(ha);
5966                 rval = ha->isp_ops->fabric_login(vha, loop_id, 0xff, 0xff,
5967                     0xfc, mb, BIT_1|BIT_0);
5968                 if (rval != QLA_SUCCESS || mb[0] != MBS_COMMAND_COMPLETE) {
5969                         ql_dbg(ql_dbg_disc, vha, 0x20a1,
5970                             "Failed SNS login: loop_id=%x mb[0]=%x mb[1]=%x mb[2]=%x mb[6]=%x mb[7]=%x (%x).\n",
5971                             loop_id, mb[0], mb[1], mb[2], mb[6], mb[7], rval);
5972                         set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
5973                         return rval;
5974                 }
5975
5976                 /* FDMI support. */
5977                 if (ql2xfdmienable &&
5978                     test_and_clear_bit(REGISTER_FDMI_NEEDED, &vha->dpc_flags))
5979                         qla2x00_fdmi_register(vha);
5980
5981                 if (test_and_clear_bit(REGISTER_FC4_NEEDED, &vha->dpc_flags)) {
5982                         if (qla2x00_rft_id(vha)) {
5983                                 /* EMPTY */
5984                                 ql_dbg(ql_dbg_disc, vha, 0x20a2,
5985                                     "Register FC-4 TYPE failed.\n");
5986                                 if (test_bit(LOOP_RESYNC_NEEDED,
5987                                     &vha->dpc_flags))
5988                                         break;
5989                         }
5990                         if (qla2x00_rff_id(vha, FC4_TYPE_FCP_SCSI)) {
5991                                 /* EMPTY */
5992                                 ql_dbg(ql_dbg_disc, vha, 0x209a,
5993                                     "Register FC-4 Features failed.\n");
5994                                 if (test_bit(LOOP_RESYNC_NEEDED,
5995                                     &vha->dpc_flags))
5996                                         break;
5997                         }
5998                         if (vha->flags.nvme_enabled) {
5999                                 if (qla2x00_rff_id(vha, FC_TYPE_NVME)) {
6000                                         ql_dbg(ql_dbg_disc, vha, 0x2049,
6001                                             "Register NVME FC Type Features failed.\n");
6002                                 }
6003                         }
6004                         if (qla2x00_rnn_id(vha)) {
6005                                 /* EMPTY */
6006                                 ql_dbg(ql_dbg_disc, vha, 0x2104,
6007                                     "Register Node Name failed.\n");
6008                                 if (test_bit(LOOP_RESYNC_NEEDED,
6009                                     &vha->dpc_flags))
6010                                         break;
6011                         } else if (qla2x00_rsnn_nn(vha)) {
6012                                 /* EMPTY */
6013                                 ql_dbg(ql_dbg_disc, vha, 0x209b,
6014                                     "Register Symbolic Node Name failed.\n");
6015                                 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
6016                                         break;
6017                         }
6018                 }
6019
6020
6021                 /* Mark the time right before querying FW for connected ports.
6022                  * This process is long, asynchronous and by the time it's done,
6023                  * collected information might not be accurate anymore. E.g.
6024                  * disconnected port might have re-connected and a brand new
6025                  * session has been created. In this case session's generation
6026                  * will be newer than discovery_gen. */
6027                 qlt_do_generation_tick(vha, &discovery_gen);
6028
6029                 if (USE_ASYNC_SCAN(ha)) {
6030                         rval = qla24xx_async_gpnft(vha, FC4_TYPE_FCP_SCSI,
6031                             NULL);
6032                         if (rval)
6033                                 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
6034                 } else  {
6035                         list_for_each_entry(fcport, &vha->vp_fcports, list)
6036                                 fcport->scan_state = QLA_FCPORT_SCAN;
6037
6038                         rval = qla2x00_find_all_fabric_devs(vha);
6039                 }
6040                 if (rval != QLA_SUCCESS)
6041                         break;
6042         } while (0);
6043
6044         if (!vha->nvme_local_port && vha->flags.nvme_enabled)
6045                 qla_nvme_register_hba(vha);
6046
6047         if (rval)
6048                 ql_dbg(ql_dbg_disc, vha, 0x2068,
6049                     "Configure fabric error exit rval=%d.\n", rval);
6050
6051         return (rval);
6052 }
6053
6054 /*
6055  * qla2x00_find_all_fabric_devs
6056  *
6057  * Input:
6058  *      ha = adapter block pointer.
6059  *      dev = database device entry pointer.
6060  *
6061  * Returns:
6062  *      0 = success.
6063  *
6064  * Context:
6065  *      Kernel context.
6066  */
6067 static int
6068 qla2x00_find_all_fabric_devs(scsi_qla_host_t *vha)
6069 {
6070         int             rval;
6071         uint16_t        loop_id;
6072         fc_port_t       *fcport, *new_fcport;
6073         int             found;
6074
6075         sw_info_t       *swl;
6076         int             swl_idx;
6077         int             first_dev, last_dev;
6078         port_id_t       wrap = {}, nxt_d_id;
6079         struct qla_hw_data *ha = vha->hw;
6080         struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
6081         unsigned long flags;
6082
6083         rval = QLA_SUCCESS;
6084
6085         /* Try GID_PT to get device list, else GAN. */
6086         if (!ha->swl)
6087                 ha->swl = kcalloc(ha->max_fibre_devices, sizeof(sw_info_t),
6088                     GFP_KERNEL);
6089         swl = ha->swl;
6090         if (!swl) {
6091                 /*EMPTY*/
6092                 ql_dbg(ql_dbg_disc, vha, 0x209c,
6093                     "GID_PT allocations failed, fallback on GA_NXT.\n");
6094         } else {
6095                 memset(swl, 0, ha->max_fibre_devices * sizeof(sw_info_t));
6096                 if (qla2x00_gid_pt(vha, swl) != QLA_SUCCESS) {
6097                         swl = NULL;
6098                         if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
6099                                 return rval;
6100                 } else if (qla2x00_gpn_id(vha, swl) != QLA_SUCCESS) {
6101                         swl = NULL;
6102                         if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
6103                                 return rval;
6104                 } else if (qla2x00_gnn_id(vha, swl) != QLA_SUCCESS) {
6105                         swl = NULL;
6106                         if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
6107                                 return rval;
6108                 } else if (qla2x00_gfpn_id(vha, swl) != QLA_SUCCESS) {
6109                         swl = NULL;
6110                         if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
6111                                 return rval;
6112                 }
6113
6114                 /* If other queries succeeded probe for FC-4 type */
6115                 if (swl) {
6116                         qla2x00_gff_id(vha, swl);
6117                         if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
6118                                 return rval;
6119                 }
6120         }
6121         swl_idx = 0;
6122
6123         /* Allocate temporary fcport for any new fcports discovered. */
6124         new_fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
6125         if (new_fcport == NULL) {
6126                 ql_log(ql_log_warn, vha, 0x209d,
6127                     "Failed to allocate memory for fcport.\n");
6128                 return (QLA_MEMORY_ALLOC_FAILED);
6129         }
6130         new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
6131         /* Set start port ID scan at adapter ID. */
6132         first_dev = 1;
6133         last_dev = 0;
6134
6135         /* Starting free loop ID. */
6136         loop_id = ha->min_external_loopid;
6137         for (; loop_id <= ha->max_loop_id; loop_id++) {
6138                 if (qla2x00_is_reserved_id(vha, loop_id))
6139                         continue;
6140
6141                 if (ha->current_topology == ISP_CFG_FL &&
6142                     (atomic_read(&vha->loop_down_timer) ||
6143                      LOOP_TRANSITION(vha))) {
6144                         atomic_set(&vha->loop_down_timer, 0);
6145                         set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
6146                         set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
6147                         break;
6148                 }
6149
6150                 if (swl != NULL) {
6151                         if (last_dev) {
6152                                 wrap.b24 = new_fcport->d_id.b24;
6153                         } else {
6154                                 new_fcport->d_id.b24 = swl[swl_idx].d_id.b24;
6155                                 memcpy(new_fcport->node_name,
6156                                     swl[swl_idx].node_name, WWN_SIZE);
6157                                 memcpy(new_fcport->port_name,
6158                                     swl[swl_idx].port_name, WWN_SIZE);
6159                                 memcpy(new_fcport->fabric_port_name,
6160                                     swl[swl_idx].fabric_port_name, WWN_SIZE);
6161                                 new_fcport->fp_speed = swl[swl_idx].fp_speed;
6162                                 new_fcport->fc4_type = swl[swl_idx].fc4_type;
6163
6164                                 new_fcport->nvme_flag = 0;
6165                                 if (vha->flags.nvme_enabled &&
6166                                     swl[swl_idx].fc4_type & FS_FC4TYPE_NVME) {
6167                                         ql_log(ql_log_info, vha, 0x2131,
6168                                             "FOUND: NVME port %8phC as FC Type 28h\n",
6169                                             new_fcport->port_name);
6170                                 }
6171
6172                                 if (swl[swl_idx].d_id.b.rsvd_1 != 0) {
6173                                         last_dev = 1;
6174                                 }
6175                                 swl_idx++;
6176                         }
6177                 } else {
6178                         /* Send GA_NXT to the switch */
6179                         rval = qla2x00_ga_nxt(vha, new_fcport);
6180                         if (rval != QLA_SUCCESS) {
6181                                 ql_log(ql_log_warn, vha, 0x209e,
6182                                     "SNS scan failed -- assuming "
6183                                     "zero-entry result.\n");
6184                                 rval = QLA_SUCCESS;
6185                                 break;
6186                         }
6187                 }
6188
6189                 /* If wrap on switch device list, exit. */
6190                 if (first_dev) {
6191                         wrap.b24 = new_fcport->d_id.b24;
6192                         first_dev = 0;
6193                 } else if (new_fcport->d_id.b24 == wrap.b24) {
6194                         ql_dbg(ql_dbg_disc, vha, 0x209f,
6195                             "Device wrap (%02x%02x%02x).\n",
6196                             new_fcport->d_id.b.domain,
6197                             new_fcport->d_id.b.area,
6198                             new_fcport->d_id.b.al_pa);
6199                         break;
6200                 }
6201
6202                 /* Bypass if same physical adapter. */
6203                 if (new_fcport->d_id.b24 == base_vha->d_id.b24)
6204                         continue;
6205
6206                 /* Bypass virtual ports of the same host. */
6207                 if (qla2x00_is_a_vp_did(vha, new_fcport->d_id.b24))
6208                         continue;
6209
6210                 /* Bypass if same domain and area of adapter. */
6211                 if (((new_fcport->d_id.b24 & 0xffff00) ==
6212                     (vha->d_id.b24 & 0xffff00)) && ha->current_topology ==
6213                         ISP_CFG_FL)
6214                             continue;
6215
6216                 /* Bypass reserved domain fields. */
6217                 if ((new_fcport->d_id.b.domain & 0xf0) == 0xf0)
6218                         continue;
6219
6220                 /* Bypass ports whose FCP-4 type is not FCP_SCSI */
6221                 if (ql2xgffidenable &&
6222                     (!(new_fcport->fc4_type & FS_FC4TYPE_FCP) &&
6223                     new_fcport->fc4_type != 0))
6224                         continue;
6225
6226                 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
6227
6228                 /* Locate matching device in database. */
6229                 found = 0;
6230                 list_for_each_entry(fcport, &vha->vp_fcports, list) {
6231                         if (memcmp(new_fcport->port_name, fcport->port_name,
6232                             WWN_SIZE))
6233                                 continue;
6234
6235                         fcport->scan_state = QLA_FCPORT_FOUND;
6236
6237                         found++;
6238
6239                         /* Update port state. */
6240                         memcpy(fcport->fabric_port_name,
6241                             new_fcport->fabric_port_name, WWN_SIZE);
6242                         fcport->fp_speed = new_fcport->fp_speed;
6243
6244                         /*
6245                          * If address the same and state FCS_ONLINE
6246                          * (or in target mode), nothing changed.
6247                          */
6248                         if (fcport->d_id.b24 == new_fcport->d_id.b24 &&
6249                             (atomic_read(&fcport->state) == FCS_ONLINE ||
6250                              (vha->host->active_mode == MODE_TARGET))) {
6251                                 break;
6252                         }
6253
6254                         if (fcport->login_retry == 0)
6255                                 fcport->login_retry =
6256                                         vha->hw->login_retry_count;
6257                         /*
6258                          * If device was not a fabric device before.
6259                          */
6260                         if ((fcport->flags & FCF_FABRIC_DEVICE) == 0) {
6261                                 fcport->d_id.b24 = new_fcport->d_id.b24;
6262                                 qla2x00_clear_loop_id(fcport);
6263                                 fcport->flags |= (FCF_FABRIC_DEVICE |
6264                                     FCF_LOGIN_NEEDED);
6265                                 break;
6266                         }
6267
6268                         /*
6269                          * Port ID changed or device was marked to be updated;
6270                          * Log it out if still logged in and mark it for
6271                          * relogin later.
6272                          */
6273                         if (qla_tgt_mode_enabled(base_vha)) {
6274                                 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf080,
6275                                          "port changed FC ID, %8phC"
6276                                          " old %x:%x:%x (loop_id 0x%04x)-> new %x:%x:%x\n",
6277                                          fcport->port_name,
6278                                          fcport->d_id.b.domain,
6279                                          fcport->d_id.b.area,
6280                                          fcport->d_id.b.al_pa,
6281                                          fcport->loop_id,
6282                                          new_fcport->d_id.b.domain,
6283                                          new_fcport->d_id.b.area,
6284                                          new_fcport->d_id.b.al_pa);
6285                                 fcport->d_id.b24 = new_fcport->d_id.b24;
6286                                 break;
6287                         }
6288
6289                         fcport->d_id.b24 = new_fcport->d_id.b24;
6290                         fcport->flags |= FCF_LOGIN_NEEDED;
6291                         break;
6292                 }
6293
6294                 if (found && NVME_TARGET(vha->hw, fcport)) {
6295                         if (fcport->disc_state == DSC_DELETE_PEND) {
6296                                 qla2x00_set_fcport_disc_state(fcport, DSC_GNL);
6297                                 vha->fcport_count--;
6298                                 fcport->login_succ = 0;
6299                         }
6300                 }
6301
6302                 if (found) {
6303                         spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
6304                         continue;
6305                 }
6306                 /* If device was not in our fcports list, then add it. */
6307                 new_fcport->scan_state = QLA_FCPORT_FOUND;
6308                 list_add_tail(&new_fcport->list, &vha->vp_fcports);
6309
6310                 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
6311
6312
6313                 /* Allocate a new replacement fcport. */
6314                 nxt_d_id.b24 = new_fcport->d_id.b24;
6315                 new_fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
6316                 if (new_fcport == NULL) {
6317                         ql_log(ql_log_warn, vha, 0xd032,
6318                             "Memory allocation failed for fcport.\n");
6319                         return (QLA_MEMORY_ALLOC_FAILED);
6320                 }
6321                 new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
6322                 new_fcport->d_id.b24 = nxt_d_id.b24;
6323         }
6324
6325         qla2x00_free_fcport(new_fcport);
6326
6327         /*
6328          * Logout all previous fabric dev marked lost, except FCP2 devices.
6329          */
6330         list_for_each_entry(fcport, &vha->vp_fcports, list) {
6331                 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
6332                         break;
6333
6334                 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0)
6335                         continue;
6336
6337                 if (fcport->scan_state == QLA_FCPORT_SCAN) {
6338                         if ((qla_dual_mode_enabled(vha) ||
6339                             qla_ini_mode_enabled(vha)) &&
6340                             atomic_read(&fcport->state) == FCS_ONLINE) {
6341                                 qla2x00_mark_device_lost(vha, fcport,
6342                                         ql2xplogiabsentdevice);
6343                                 if (fcport->loop_id != FC_NO_LOOP_ID &&
6344                                     (fcport->flags & FCF_FCP2_DEVICE) == 0 &&
6345                                     fcport->port_type != FCT_INITIATOR &&
6346                                     fcport->port_type != FCT_BROADCAST) {
6347                                         ql_dbg(ql_dbg_disc, vha, 0x20f0,
6348                                             "%s %d %8phC post del sess\n",
6349                                             __func__, __LINE__,
6350                                             fcport->port_name);
6351                                         qlt_schedule_sess_for_deletion(fcport);
6352                                         continue;
6353                                 }
6354                         }
6355                 }
6356
6357                 if (fcport->scan_state == QLA_FCPORT_FOUND &&
6358                     (fcport->flags & FCF_LOGIN_NEEDED) != 0)
6359                         qla24xx_fcport_handle_login(vha, fcport);
6360         }
6361         return (rval);
6362 }
6363
6364 /* FW does not set aside Loop id for MGMT Server/FFFFFAh */
6365 int
6366 qla2x00_reserve_mgmt_server_loop_id(scsi_qla_host_t *vha)
6367 {
6368         int loop_id = FC_NO_LOOP_ID;
6369         int lid = NPH_MGMT_SERVER - vha->vp_idx;
6370         unsigned long flags;
6371         struct qla_hw_data *ha = vha->hw;
6372
6373         if (vha->vp_idx == 0) {
6374                 set_bit(NPH_MGMT_SERVER, ha->loop_id_map);
6375                 return NPH_MGMT_SERVER;
6376         }
6377
6378         /* pick id from high and work down to low */
6379         spin_lock_irqsave(&ha->vport_slock, flags);
6380         for (; lid > 0; lid--) {
6381                 if (!test_bit(lid, vha->hw->loop_id_map)) {
6382                         set_bit(lid, vha->hw->loop_id_map);
6383                         loop_id = lid;
6384                         break;
6385                 }
6386         }
6387         spin_unlock_irqrestore(&ha->vport_slock, flags);
6388
6389         return loop_id;
6390 }
6391
6392 /*
6393  * qla2x00_fabric_login
6394  *      Issue fabric login command.
6395  *
6396  * Input:
6397  *      ha = adapter block pointer.
6398  *      device = pointer to FC device type structure.
6399  *
6400  * Returns:
6401  *      0 - Login successfully
6402  *      1 - Login failed
6403  *      2 - Initiator device
6404  *      3 - Fatal error
6405  */
6406 int
6407 qla2x00_fabric_login(scsi_qla_host_t *vha, fc_port_t *fcport,
6408     uint16_t *next_loopid)
6409 {
6410         int     rval;
6411         int     retry;
6412         uint16_t tmp_loopid;
6413         uint16_t mb[MAILBOX_REGISTER_COUNT];
6414         struct qla_hw_data *ha = vha->hw;
6415
6416         retry = 0;
6417         tmp_loopid = 0;
6418
6419         for (;;) {
6420                 ql_dbg(ql_dbg_disc, vha, 0x2000,
6421                     "Trying Fabric Login w/loop id 0x%04x for port "
6422                     "%02x%02x%02x.\n",
6423                     fcport->loop_id, fcport->d_id.b.domain,
6424                     fcport->d_id.b.area, fcport->d_id.b.al_pa);
6425
6426                 /* Login fcport on switch. */
6427                 rval = ha->isp_ops->fabric_login(vha, fcport->loop_id,
6428                     fcport->d_id.b.domain, fcport->d_id.b.area,
6429                     fcport->d_id.b.al_pa, mb, BIT_0);
6430                 if (rval != QLA_SUCCESS) {
6431                         return rval;
6432                 }
6433                 if (mb[0] == MBS_PORT_ID_USED) {
6434                         /*
6435                          * Device has another loop ID.  The firmware team
6436                          * recommends the driver perform an implicit login with
6437                          * the specified ID again. The ID we just used is save
6438                          * here so we return with an ID that can be tried by
6439                          * the next login.
6440                          */
6441                         retry++;
6442                         tmp_loopid = fcport->loop_id;
6443                         fcport->loop_id = mb[1];
6444
6445                         ql_dbg(ql_dbg_disc, vha, 0x2001,
6446                             "Fabric Login: port in use - next loop "
6447                             "id=0x%04x, port id= %02x%02x%02x.\n",
6448                             fcport->loop_id, fcport->d_id.b.domain,
6449                             fcport->d_id.b.area, fcport->d_id.b.al_pa);
6450
6451                 } else if (mb[0] == MBS_COMMAND_COMPLETE) {
6452                         /*
6453                          * Login succeeded.
6454                          */
6455                         if (retry) {
6456                                 /* A retry occurred before. */
6457                                 *next_loopid = tmp_loopid;
6458                         } else {
6459                                 /*
6460                                  * No retry occurred before. Just increment the
6461                                  * ID value for next login.
6462                                  */
6463                                 *next_loopid = (fcport->loop_id + 1);
6464                         }
6465
6466                         if (mb[1] & BIT_0) {
6467                                 fcport->port_type = FCT_INITIATOR;
6468                         } else {
6469                                 fcport->port_type = FCT_TARGET;
6470                                 if (mb[1] & BIT_1) {
6471                                         fcport->flags |= FCF_FCP2_DEVICE;
6472                                 }
6473                         }
6474
6475                         if (mb[10] & BIT_0)
6476                                 fcport->supported_classes |= FC_COS_CLASS2;
6477                         if (mb[10] & BIT_1)
6478                                 fcport->supported_classes |= FC_COS_CLASS3;
6479
6480                         if (IS_FWI2_CAPABLE(ha)) {
6481                                 if (mb[10] & BIT_7)
6482                                         fcport->flags |=
6483                                             FCF_CONF_COMP_SUPPORTED;
6484                         }
6485
6486                         rval = QLA_SUCCESS;
6487                         break;
6488                 } else if (mb[0] == MBS_LOOP_ID_USED) {
6489                         /*
6490                          * Loop ID already used, try next loop ID.
6491                          */
6492                         fcport->loop_id++;
6493                         rval = qla2x00_find_new_loop_id(vha, fcport);
6494                         if (rval != QLA_SUCCESS) {
6495                                 /* Ran out of loop IDs to use */
6496                                 break;
6497                         }
6498                 } else if (mb[0] == MBS_COMMAND_ERROR) {
6499                         /*
6500                          * Firmware possibly timed out during login. If NO
6501                          * retries are left to do then the device is declared
6502                          * dead.
6503                          */
6504                         *next_loopid = fcport->loop_id;
6505                         ha->isp_ops->fabric_logout(vha, fcport->loop_id,
6506                             fcport->d_id.b.domain, fcport->d_id.b.area,
6507                             fcport->d_id.b.al_pa);
6508                         qla2x00_mark_device_lost(vha, fcport, 1);
6509
6510                         rval = 1;
6511                         break;
6512                 } else {
6513                         /*
6514                          * unrecoverable / not handled error
6515                          */
6516                         ql_dbg(ql_dbg_disc, vha, 0x2002,
6517                             "Failed=%x port_id=%02x%02x%02x loop_id=%x "
6518                             "jiffies=%lx.\n", mb[0], fcport->d_id.b.domain,
6519                             fcport->d_id.b.area, fcport->d_id.b.al_pa,
6520                             fcport->loop_id, jiffies);
6521
6522                         *next_loopid = fcport->loop_id;
6523                         ha->isp_ops->fabric_logout(vha, fcport->loop_id,
6524                             fcport->d_id.b.domain, fcport->d_id.b.area,
6525                             fcport->d_id.b.al_pa);
6526                         qla2x00_clear_loop_id(fcport);
6527                         fcport->login_retry = 0;
6528
6529                         rval = 3;
6530                         break;
6531                 }
6532         }
6533
6534         return (rval);
6535 }
6536
6537 /*
6538  * qla2x00_local_device_login
6539  *      Issue local device login command.
6540  *
6541  * Input:
6542  *      ha = adapter block pointer.
6543  *      loop_id = loop id of device to login to.
6544  *
6545  * Returns (Where's the #define!!!!):
6546  *      0 - Login successfully
6547  *      1 - Login failed
6548  *      3 - Fatal error
6549  */
6550 int
6551 qla2x00_local_device_login(scsi_qla_host_t *vha, fc_port_t *fcport)
6552 {
6553         int             rval;
6554         uint16_t        mb[MAILBOX_REGISTER_COUNT];
6555
6556         memset(mb, 0, sizeof(mb));
6557         rval = qla2x00_login_local_device(vha, fcport, mb, BIT_0);
6558         if (rval == QLA_SUCCESS) {
6559                 /* Interrogate mailbox registers for any errors */
6560                 if (mb[0] == MBS_COMMAND_ERROR)
6561                         rval = 1;
6562                 else if (mb[0] == MBS_COMMAND_PARAMETER_ERROR)
6563                         /* device not in PCB table */
6564                         rval = 3;
6565         }
6566
6567         return (rval);
6568 }
6569
6570 /*
6571  *  qla2x00_loop_resync
6572  *      Resync with fibre channel devices.
6573  *
6574  * Input:
6575  *      ha = adapter block pointer.
6576  *
6577  * Returns:
6578  *      0 = success
6579  */
6580 int
6581 qla2x00_loop_resync(scsi_qla_host_t *vha)
6582 {
6583         int rval = QLA_SUCCESS;
6584         uint32_t wait_time;
6585
6586         clear_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
6587         if (vha->flags.online) {
6588                 if (!(rval = qla2x00_fw_ready(vha))) {
6589                         /* Wait at most MAX_TARGET RSCNs for a stable link. */
6590                         wait_time = 256;
6591                         do {
6592                                 if (!IS_QLAFX00(vha->hw)) {
6593                                         /*
6594                                          * Issue a marker after FW becomes
6595                                          * ready.
6596                                          */
6597                                         qla2x00_marker(vha, vha->hw->base_qpair,
6598                                             0, 0, MK_SYNC_ALL);
6599                                         vha->marker_needed = 0;
6600                                 }
6601
6602                                 /* Remap devices on Loop. */
6603                                 clear_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
6604
6605                                 if (IS_QLAFX00(vha->hw))
6606                                         qlafx00_configure_devices(vha);
6607                                 else
6608                                         qla2x00_configure_loop(vha);
6609
6610                                 wait_time--;
6611                         } while (!atomic_read(&vha->loop_down_timer) &&
6612                                 !(test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags))
6613                                 && wait_time && (test_bit(LOOP_RESYNC_NEEDED,
6614                                 &vha->dpc_flags)));
6615                 }
6616         }
6617
6618         if (test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags))
6619                 return (QLA_FUNCTION_FAILED);
6620
6621         if (rval)
6622                 ql_dbg(ql_dbg_disc, vha, 0x206c,
6623                     "%s *** FAILED ***.\n", __func__);
6624
6625         return (rval);
6626 }
6627
6628 /*
6629 * qla2x00_perform_loop_resync
6630 * Description: This function will set the appropriate flags and call
6631 *              qla2x00_loop_resync. If successful loop will be resynced
6632 * Arguments : scsi_qla_host_t pointer
6633 * returm    : Success or Failure
6634 */
6635
6636 int qla2x00_perform_loop_resync(scsi_qla_host_t *ha)
6637 {
6638         int32_t rval = 0;
6639
6640         if (!test_and_set_bit(LOOP_RESYNC_ACTIVE, &ha->dpc_flags)) {
6641                 /*Configure the flags so that resync happens properly*/
6642                 atomic_set(&ha->loop_down_timer, 0);
6643                 if (!(ha->device_flags & DFLG_NO_CABLE)) {
6644                         atomic_set(&ha->loop_state, LOOP_UP);
6645                         set_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
6646                         set_bit(REGISTER_FC4_NEEDED, &ha->dpc_flags);
6647                         set_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
6648
6649                         rval = qla2x00_loop_resync(ha);
6650                 } else
6651                         atomic_set(&ha->loop_state, LOOP_DEAD);
6652
6653                 clear_bit(LOOP_RESYNC_ACTIVE, &ha->dpc_flags);
6654         }
6655
6656         return rval;
6657 }
6658
6659 void
6660 qla2x00_update_fcports(scsi_qla_host_t *base_vha)
6661 {
6662         fc_port_t *fcport;
6663         struct scsi_qla_host *vha, *tvp;
6664         struct qla_hw_data *ha = base_vha->hw;
6665         unsigned long flags;
6666
6667         spin_lock_irqsave(&ha->vport_slock, flags);
6668         /* Go with deferred removal of rport references. */
6669         list_for_each_entry_safe(vha, tvp, &base_vha->hw->vp_list, list) {
6670                 atomic_inc(&vha->vref_count);
6671                 list_for_each_entry(fcport, &vha->vp_fcports, list) {
6672                         if (fcport->drport &&
6673                             atomic_read(&fcport->state) != FCS_UNCONFIGURED) {
6674                                 spin_unlock_irqrestore(&ha->vport_slock, flags);
6675                                 qla2x00_rport_del(fcport);
6676
6677                                 spin_lock_irqsave(&ha->vport_slock, flags);
6678                         }
6679                 }
6680                 atomic_dec(&vha->vref_count);
6681                 wake_up(&vha->vref_waitq);
6682         }
6683         spin_unlock_irqrestore(&ha->vport_slock, flags);
6684 }
6685
6686 /* Assumes idc_lock always held on entry */
6687 void
6688 qla83xx_reset_ownership(scsi_qla_host_t *vha)
6689 {
6690         struct qla_hw_data *ha = vha->hw;
6691         uint32_t drv_presence, drv_presence_mask;
6692         uint32_t dev_part_info1, dev_part_info2, class_type;
6693         uint32_t class_type_mask = 0x3;
6694         uint16_t fcoe_other_function = 0xffff, i;
6695
6696         if (IS_QLA8044(ha)) {
6697                 drv_presence = qla8044_rd_direct(vha,
6698                     QLA8044_CRB_DRV_ACTIVE_INDEX);
6699                 dev_part_info1 = qla8044_rd_direct(vha,
6700                     QLA8044_CRB_DEV_PART_INFO_INDEX);
6701                 dev_part_info2 = qla8044_rd_direct(vha,
6702                     QLA8044_CRB_DEV_PART_INFO2);
6703         } else {
6704                 qla83xx_rd_reg(vha, QLA83XX_IDC_DRV_PRESENCE, &drv_presence);
6705                 qla83xx_rd_reg(vha, QLA83XX_DEV_PARTINFO1, &dev_part_info1);
6706                 qla83xx_rd_reg(vha, QLA83XX_DEV_PARTINFO2, &dev_part_info2);
6707         }
6708         for (i = 0; i < 8; i++) {
6709                 class_type = ((dev_part_info1 >> (i * 4)) & class_type_mask);
6710                 if ((class_type == QLA83XX_CLASS_TYPE_FCOE) &&
6711                     (i != ha->portnum)) {
6712                         fcoe_other_function = i;
6713                         break;
6714                 }
6715         }
6716         if (fcoe_other_function == 0xffff) {
6717                 for (i = 0; i < 8; i++) {
6718                         class_type = ((dev_part_info2 >> (i * 4)) &
6719                             class_type_mask);
6720                         if ((class_type == QLA83XX_CLASS_TYPE_FCOE) &&
6721                             ((i + 8) != ha->portnum)) {
6722                                 fcoe_other_function = i + 8;
6723                                 break;
6724                         }
6725                 }
6726         }
6727         /*
6728          * Prepare drv-presence mask based on fcoe functions present.
6729          * However consider only valid physical fcoe function numbers (0-15).
6730          */
6731         drv_presence_mask = ~((1 << (ha->portnum)) |
6732                         ((fcoe_other_function == 0xffff) ?
6733                          0 : (1 << (fcoe_other_function))));
6734
6735         /* We are the reset owner iff:
6736          *    - No other protocol drivers present.
6737          *    - This is the lowest among fcoe functions. */
6738         if (!(drv_presence & drv_presence_mask) &&
6739                         (ha->portnum < fcoe_other_function)) {
6740                 ql_dbg(ql_dbg_p3p, vha, 0xb07f,
6741                     "This host is Reset owner.\n");
6742                 ha->flags.nic_core_reset_owner = 1;
6743         }
6744 }
6745
6746 static int
6747 __qla83xx_set_drv_ack(scsi_qla_host_t *vha)
6748 {
6749         int rval = QLA_SUCCESS;
6750         struct qla_hw_data *ha = vha->hw;
6751         uint32_t drv_ack;
6752
6753         rval = qla83xx_rd_reg(vha, QLA83XX_IDC_DRIVER_ACK, &drv_ack);
6754         if (rval == QLA_SUCCESS) {
6755                 drv_ack |= (1 << ha->portnum);
6756                 rval = qla83xx_wr_reg(vha, QLA83XX_IDC_DRIVER_ACK, drv_ack);
6757         }
6758
6759         return rval;
6760 }
6761
6762 static int
6763 __qla83xx_clear_drv_ack(scsi_qla_host_t *vha)
6764 {
6765         int rval = QLA_SUCCESS;
6766         struct qla_hw_data *ha = vha->hw;
6767         uint32_t drv_ack;
6768
6769         rval = qla83xx_rd_reg(vha, QLA83XX_IDC_DRIVER_ACK, &drv_ack);
6770         if (rval == QLA_SUCCESS) {
6771                 drv_ack &= ~(1 << ha->portnum);
6772                 rval = qla83xx_wr_reg(vha, QLA83XX_IDC_DRIVER_ACK, drv_ack);
6773         }
6774
6775         return rval;
6776 }
6777
6778 static const char *
6779 qla83xx_dev_state_to_string(uint32_t dev_state)
6780 {
6781         switch (dev_state) {
6782         case QLA8XXX_DEV_COLD:
6783                 return "COLD/RE-INIT";
6784         case QLA8XXX_DEV_INITIALIZING:
6785                 return "INITIALIZING";
6786         case QLA8XXX_DEV_READY:
6787                 return "READY";
6788         case QLA8XXX_DEV_NEED_RESET:
6789                 return "NEED RESET";
6790         case QLA8XXX_DEV_NEED_QUIESCENT:
6791                 return "NEED QUIESCENT";
6792         case QLA8XXX_DEV_FAILED:
6793                 return "FAILED";
6794         case QLA8XXX_DEV_QUIESCENT:
6795                 return "QUIESCENT";
6796         default:
6797                 return "Unknown";
6798         }
6799 }
6800
6801 /* Assumes idc-lock always held on entry */
6802 void
6803 qla83xx_idc_audit(scsi_qla_host_t *vha, int audit_type)
6804 {
6805         struct qla_hw_data *ha = vha->hw;
6806         uint32_t idc_audit_reg = 0, duration_secs = 0;
6807
6808         switch (audit_type) {
6809         case IDC_AUDIT_TIMESTAMP:
6810                 ha->idc_audit_ts = (jiffies_to_msecs(jiffies) / 1000);
6811                 idc_audit_reg = (ha->portnum) |
6812                     (IDC_AUDIT_TIMESTAMP << 7) | (ha->idc_audit_ts << 8);
6813                 qla83xx_wr_reg(vha, QLA83XX_IDC_AUDIT, idc_audit_reg);
6814                 break;
6815
6816         case IDC_AUDIT_COMPLETION:
6817                 duration_secs = ((jiffies_to_msecs(jiffies) -
6818                     jiffies_to_msecs(ha->idc_audit_ts)) / 1000);
6819                 idc_audit_reg = (ha->portnum) |
6820                     (IDC_AUDIT_COMPLETION << 7) | (duration_secs << 8);
6821                 qla83xx_wr_reg(vha, QLA83XX_IDC_AUDIT, idc_audit_reg);
6822                 break;
6823
6824         default:
6825                 ql_log(ql_log_warn, vha, 0xb078,
6826                     "Invalid audit type specified.\n");
6827                 break;
6828         }
6829 }
6830
6831 /* Assumes idc_lock always held on entry */
6832 static int
6833 qla83xx_initiating_reset(scsi_qla_host_t *vha)
6834 {
6835         struct qla_hw_data *ha = vha->hw;
6836         uint32_t  idc_control, dev_state;
6837
6838         __qla83xx_get_idc_control(vha, &idc_control);
6839         if ((idc_control & QLA83XX_IDC_RESET_DISABLED)) {
6840                 ql_log(ql_log_info, vha, 0xb080,
6841                     "NIC Core reset has been disabled. idc-control=0x%x\n",
6842                     idc_control);
6843                 return QLA_FUNCTION_FAILED;
6844         }
6845
6846         /* Set NEED-RESET iff in READY state and we are the reset-owner */
6847         qla83xx_rd_reg(vha, QLA83XX_IDC_DEV_STATE, &dev_state);
6848         if (ha->flags.nic_core_reset_owner && dev_state == QLA8XXX_DEV_READY) {
6849                 qla83xx_wr_reg(vha, QLA83XX_IDC_DEV_STATE,
6850                     QLA8XXX_DEV_NEED_RESET);
6851                 ql_log(ql_log_info, vha, 0xb056, "HW State: NEED RESET.\n");
6852                 qla83xx_idc_audit(vha, IDC_AUDIT_TIMESTAMP);
6853         } else {
6854                 const char *state = qla83xx_dev_state_to_string(dev_state);
6855
6856                 ql_log(ql_log_info, vha, 0xb057, "HW State: %s.\n", state);
6857
6858                 /* SV: XXX: Is timeout required here? */
6859                 /* Wait for IDC state change READY -> NEED_RESET */
6860                 while (dev_state == QLA8XXX_DEV_READY) {
6861                         qla83xx_idc_unlock(vha, 0);
6862                         msleep(200);
6863                         qla83xx_idc_lock(vha, 0);
6864                         qla83xx_rd_reg(vha, QLA83XX_IDC_DEV_STATE, &dev_state);
6865                 }
6866         }
6867
6868         /* Send IDC ack by writing to drv-ack register */
6869         __qla83xx_set_drv_ack(vha);
6870
6871         return QLA_SUCCESS;
6872 }
6873
6874 int
6875 __qla83xx_set_idc_control(scsi_qla_host_t *vha, uint32_t idc_control)
6876 {
6877         return qla83xx_wr_reg(vha, QLA83XX_IDC_CONTROL, idc_control);
6878 }
6879
6880 int
6881 __qla83xx_get_idc_control(scsi_qla_host_t *vha, uint32_t *idc_control)
6882 {
6883         return qla83xx_rd_reg(vha, QLA83XX_IDC_CONTROL, idc_control);
6884 }
6885
6886 static int
6887 qla83xx_check_driver_presence(scsi_qla_host_t *vha)
6888 {
6889         uint32_t drv_presence = 0;
6890         struct qla_hw_data *ha = vha->hw;
6891
6892         qla83xx_rd_reg(vha, QLA83XX_IDC_DRV_PRESENCE, &drv_presence);
6893         if (drv_presence & (1 << ha->portnum))
6894                 return QLA_SUCCESS;
6895         else
6896                 return QLA_TEST_FAILED;
6897 }
6898
6899 int
6900 qla83xx_nic_core_reset(scsi_qla_host_t *vha)
6901 {
6902         int rval = QLA_SUCCESS;
6903         struct qla_hw_data *ha = vha->hw;
6904
6905         ql_dbg(ql_dbg_p3p, vha, 0xb058,
6906             "Entered  %s().\n", __func__);
6907
6908         if (vha->device_flags & DFLG_DEV_FAILED) {
6909                 ql_log(ql_log_warn, vha, 0xb059,
6910                     "Device in unrecoverable FAILED state.\n");
6911                 return QLA_FUNCTION_FAILED;
6912         }
6913
6914         qla83xx_idc_lock(vha, 0);
6915
6916         if (qla83xx_check_driver_presence(vha) != QLA_SUCCESS) {
6917                 ql_log(ql_log_warn, vha, 0xb05a,
6918                     "Function=0x%x has been removed from IDC participation.\n",
6919                     ha->portnum);
6920                 rval = QLA_FUNCTION_FAILED;
6921                 goto exit;
6922         }
6923
6924         qla83xx_reset_ownership(vha);
6925
6926         rval = qla83xx_initiating_reset(vha);
6927
6928         /*
6929          * Perform reset if we are the reset-owner,
6930          * else wait till IDC state changes to READY/FAILED.
6931          */
6932         if (rval == QLA_SUCCESS) {
6933                 rval = qla83xx_idc_state_handler(vha);
6934
6935                 if (rval == QLA_SUCCESS)
6936                         ha->flags.nic_core_hung = 0;
6937                 __qla83xx_clear_drv_ack(vha);
6938         }
6939
6940 exit:
6941         qla83xx_idc_unlock(vha, 0);
6942
6943         ql_dbg(ql_dbg_p3p, vha, 0xb05b, "Exiting %s.\n", __func__);
6944
6945         return rval;
6946 }
6947
6948 int
6949 qla2xxx_mctp_dump(scsi_qla_host_t *vha)
6950 {
6951         struct qla_hw_data *ha = vha->hw;
6952         int rval = QLA_FUNCTION_FAILED;
6953
6954         if (!IS_MCTP_CAPABLE(ha)) {
6955                 /* This message can be removed from the final version */
6956                 ql_log(ql_log_info, vha, 0x506d,
6957                     "This board is not MCTP capable\n");
6958                 return rval;
6959         }
6960
6961         if (!ha->mctp_dump) {
6962                 ha->mctp_dump = dma_alloc_coherent(&ha->pdev->dev,
6963                     MCTP_DUMP_SIZE, &ha->mctp_dump_dma, GFP_KERNEL);
6964
6965                 if (!ha->mctp_dump) {
6966                         ql_log(ql_log_warn, vha, 0x506e,
6967                             "Failed to allocate memory for mctp dump\n");
6968                         return rval;
6969                 }
6970         }
6971
6972 #define MCTP_DUMP_STR_ADDR      0x00000000
6973         rval = qla2x00_dump_mctp_data(vha, ha->mctp_dump_dma,
6974             MCTP_DUMP_STR_ADDR, MCTP_DUMP_SIZE/4);
6975         if (rval != QLA_SUCCESS) {
6976                 ql_log(ql_log_warn, vha, 0x506f,
6977                     "Failed to capture mctp dump\n");
6978         } else {
6979                 ql_log(ql_log_info, vha, 0x5070,
6980                     "Mctp dump capture for host (%ld/%p).\n",
6981                     vha->host_no, ha->mctp_dump);
6982                 ha->mctp_dumped = 1;
6983         }
6984
6985         if (!ha->flags.nic_core_reset_hdlr_active && !ha->portnum) {
6986                 ha->flags.nic_core_reset_hdlr_active = 1;
6987                 rval = qla83xx_restart_nic_firmware(vha);
6988                 if (rval)
6989                         /* NIC Core reset failed. */
6990                         ql_log(ql_log_warn, vha, 0x5071,
6991                             "Failed to restart nic firmware\n");
6992                 else
6993                         ql_dbg(ql_dbg_p3p, vha, 0xb084,
6994                             "Restarted NIC firmware successfully.\n");
6995                 ha->flags.nic_core_reset_hdlr_active = 0;
6996         }
6997
6998         return rval;
6999
7000 }
7001
7002 /*
7003 * qla2x00_quiesce_io
7004 * Description: This function will block the new I/Os
7005 *              Its not aborting any I/Os as context
7006 *              is not destroyed during quiescence
7007 * Arguments: scsi_qla_host_t
7008 * return   : void
7009 */
7010 void
7011 qla2x00_quiesce_io(scsi_qla_host_t *vha)
7012 {
7013         struct qla_hw_data *ha = vha->hw;
7014         struct scsi_qla_host *vp, *tvp;
7015         unsigned long flags;
7016
7017         ql_dbg(ql_dbg_dpc, vha, 0x401d,
7018             "Quiescing I/O - ha=%p.\n", ha);
7019
7020         atomic_set(&ha->loop_down_timer, LOOP_DOWN_TIME);
7021         if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
7022                 atomic_set(&vha->loop_state, LOOP_DOWN);
7023                 qla2x00_mark_all_devices_lost(vha);
7024
7025                 spin_lock_irqsave(&ha->vport_slock, flags);
7026                 list_for_each_entry_safe(vp, tvp, &ha->vp_list, list) {
7027                         atomic_inc(&vp->vref_count);
7028                         spin_unlock_irqrestore(&ha->vport_slock, flags);
7029
7030                         qla2x00_mark_all_devices_lost(vp);
7031
7032                         spin_lock_irqsave(&ha->vport_slock, flags);
7033                         atomic_dec(&vp->vref_count);
7034                 }
7035                 spin_unlock_irqrestore(&ha->vport_slock, flags);
7036         } else {
7037                 if (!atomic_read(&vha->loop_down_timer))
7038                         atomic_set(&vha->loop_down_timer,
7039                                         LOOP_DOWN_TIME);
7040         }
7041         /* Wait for pending cmds to complete */
7042         WARN_ON_ONCE(qla2x00_eh_wait_for_pending_commands(vha, 0, 0, WAIT_HOST)
7043                      != QLA_SUCCESS);
7044 }
7045
7046 void
7047 qla2x00_abort_isp_cleanup(scsi_qla_host_t *vha)
7048 {
7049         struct qla_hw_data *ha = vha->hw;
7050         struct scsi_qla_host *vp, *tvp;
7051         unsigned long flags;
7052         fc_port_t *fcport;
7053         u16 i;
7054
7055         /* For ISP82XX, driver waits for completion of the commands.
7056          * online flag should be set.
7057          */
7058         if (!(IS_P3P_TYPE(ha)))
7059                 vha->flags.online = 0;
7060         ha->flags.chip_reset_done = 0;
7061         clear_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
7062         vha->qla_stats.total_isp_aborts++;
7063
7064         ql_log(ql_log_info, vha, 0x00af,
7065             "Performing ISP error recovery - ha=%p.\n", ha);
7066
7067         ha->flags.purge_mbox = 1;
7068         /* For ISP82XX, reset_chip is just disabling interrupts.
7069          * Driver waits for the completion of the commands.
7070          * the interrupts need to be enabled.
7071          */
7072         if (!(IS_P3P_TYPE(ha)))
7073                 ha->isp_ops->reset_chip(vha);
7074
7075         ha->link_data_rate = PORT_SPEED_UNKNOWN;
7076         SAVE_TOPO(ha);
7077         ha->flags.rida_fmt2 = 0;
7078         ha->flags.n2n_ae = 0;
7079         ha->flags.lip_ae = 0;
7080         ha->current_topology = 0;
7081         QLA_FW_STOPPED(ha);
7082         ha->flags.fw_init_done = 0;
7083         ha->chip_reset++;
7084         ha->base_qpair->chip_reset = ha->chip_reset;
7085         ha->base_qpair->cmd_cnt = ha->base_qpair->cmd_completion_cnt = 0;
7086         ha->base_qpair->prev_completion_cnt = 0;
7087         for (i = 0; i < ha->max_qpairs; i++) {
7088                 if (ha->queue_pair_map[i]) {
7089                         ha->queue_pair_map[i]->chip_reset =
7090                                 ha->base_qpair->chip_reset;
7091                         ha->queue_pair_map[i]->cmd_cnt =
7092                             ha->queue_pair_map[i]->cmd_completion_cnt = 0;
7093                         ha->base_qpair->prev_completion_cnt = 0;
7094                 }
7095         }
7096
7097         /* purge MBox commands */
7098         if (atomic_read(&ha->num_pend_mbx_stage3)) {
7099                 clear_bit(MBX_INTR_WAIT, &ha->mbx_cmd_flags);
7100                 complete(&ha->mbx_intr_comp);
7101         }
7102
7103         i = 0;
7104         while (atomic_read(&ha->num_pend_mbx_stage3) ||
7105             atomic_read(&ha->num_pend_mbx_stage2) ||
7106             atomic_read(&ha->num_pend_mbx_stage1)) {
7107                 msleep(20);
7108                 i++;
7109                 if (i > 50)
7110                         break;
7111         }
7112         ha->flags.purge_mbox = 0;
7113
7114         atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
7115         if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
7116                 atomic_set(&vha->loop_state, LOOP_DOWN);
7117                 qla2x00_mark_all_devices_lost(vha);
7118
7119                 spin_lock_irqsave(&ha->vport_slock, flags);
7120                 list_for_each_entry_safe(vp, tvp, &ha->vp_list, list) {
7121                         atomic_inc(&vp->vref_count);
7122                         spin_unlock_irqrestore(&ha->vport_slock, flags);
7123
7124                         qla2x00_mark_all_devices_lost(vp);
7125
7126                         spin_lock_irqsave(&ha->vport_slock, flags);
7127                         atomic_dec(&vp->vref_count);
7128                 }
7129                 spin_unlock_irqrestore(&ha->vport_slock, flags);
7130         } else {
7131                 if (!atomic_read(&vha->loop_down_timer))
7132                         atomic_set(&vha->loop_down_timer,
7133                             LOOP_DOWN_TIME);
7134         }
7135
7136         /* Clear all async request states across all VPs. */
7137         list_for_each_entry(fcport, &vha->vp_fcports, list) {
7138                 fcport->flags &= ~(FCF_LOGIN_NEEDED | FCF_ASYNC_SENT);
7139                 fcport->scan_state = 0;
7140         }
7141         spin_lock_irqsave(&ha->vport_slock, flags);
7142         list_for_each_entry_safe(vp, tvp, &ha->vp_list, list) {
7143                 atomic_inc(&vp->vref_count);
7144                 spin_unlock_irqrestore(&ha->vport_slock, flags);
7145
7146                 list_for_each_entry(fcport, &vp->vp_fcports, list)
7147                         fcport->flags &= ~(FCF_LOGIN_NEEDED | FCF_ASYNC_SENT);
7148
7149                 spin_lock_irqsave(&ha->vport_slock, flags);
7150                 atomic_dec(&vp->vref_count);
7151         }
7152         spin_unlock_irqrestore(&ha->vport_slock, flags);
7153
7154         /* Make sure for ISP 82XX IO DMA is complete */
7155         if (IS_P3P_TYPE(ha)) {
7156                 qla82xx_chip_reset_cleanup(vha);
7157                 ql_log(ql_log_info, vha, 0x00b4,
7158                        "Done chip reset cleanup.\n");
7159
7160                 /* Done waiting for pending commands. Reset online flag */
7161                 vha->flags.online = 0;
7162         }
7163
7164         /* Requeue all commands in outstanding command list. */
7165         qla2x00_abort_all_cmds(vha, DID_RESET << 16);
7166         /* memory barrier */
7167         wmb();
7168 }
7169
7170 /*
7171 *  qla2x00_abort_isp
7172 *      Resets ISP and aborts all outstanding commands.
7173 *
7174 * Input:
7175 *      ha           = adapter block pointer.
7176 *
7177 * Returns:
7178 *      0 = success
7179 */
7180 int
7181 qla2x00_abort_isp(scsi_qla_host_t *vha)
7182 {
7183         int rval;
7184         uint8_t        status = 0;
7185         struct qla_hw_data *ha = vha->hw;
7186         struct scsi_qla_host *vp, *tvp;
7187         struct req_que *req = ha->req_q_map[0];
7188         unsigned long flags;
7189
7190         if (vha->flags.online) {
7191                 qla2x00_abort_isp_cleanup(vha);
7192
7193                 if (vha->hw->flags.port_isolated)
7194                         return status;
7195
7196                 if (qla2x00_isp_reg_stat(ha)) {
7197                         ql_log(ql_log_info, vha, 0x803f,
7198                                "ISP Abort - ISP reg disconnect, exiting.\n");
7199                         return status;
7200                 }
7201
7202                 if (test_and_clear_bit(ISP_ABORT_TO_ROM, &vha->dpc_flags)) {
7203                         ha->flags.chip_reset_done = 1;
7204                         vha->flags.online = 1;
7205                         status = 0;
7206                         clear_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
7207                         return status;
7208                 }
7209
7210                 if (IS_QLA8031(ha)) {
7211                         ql_dbg(ql_dbg_p3p, vha, 0xb05c,
7212                             "Clearing fcoe driver presence.\n");
7213                         if (qla83xx_clear_drv_presence(vha) != QLA_SUCCESS)
7214                                 ql_dbg(ql_dbg_p3p, vha, 0xb073,
7215                                     "Error while clearing DRV-Presence.\n");
7216                 }
7217
7218                 if (unlikely(pci_channel_offline(ha->pdev) &&
7219                     ha->flags.pci_channel_io_perm_failure)) {
7220                         clear_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
7221                         status = 0;
7222                         return status;
7223                 }
7224
7225                 switch (vha->qlini_mode) {
7226                 case QLA2XXX_INI_MODE_DISABLED:
7227                         if (!qla_tgt_mode_enabled(vha))
7228                                 return 0;
7229                         break;
7230                 case QLA2XXX_INI_MODE_DUAL:
7231                         if (!qla_dual_mode_enabled(vha) &&
7232                             !qla_ini_mode_enabled(vha))
7233                                 return 0;
7234                         break;
7235                 case QLA2XXX_INI_MODE_ENABLED:
7236                 default:
7237                         break;
7238                 }
7239
7240                 ha->isp_ops->get_flash_version(vha, req->ring);
7241
7242                 if (qla2x00_isp_reg_stat(ha)) {
7243                         ql_log(ql_log_info, vha, 0x803f,
7244                                "ISP Abort - ISP reg disconnect pre nvram config, exiting.\n");
7245                         return status;
7246                 }
7247                 ha->isp_ops->nvram_config(vha);
7248
7249                 if (qla2x00_isp_reg_stat(ha)) {
7250                         ql_log(ql_log_info, vha, 0x803f,
7251                                "ISP Abort - ISP reg disconnect post nvmram config, exiting.\n");
7252                         return status;
7253                 }
7254                 if (!qla2x00_restart_isp(vha)) {
7255                         clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
7256
7257                         if (!atomic_read(&vha->loop_down_timer)) {
7258                                 /*
7259                                  * Issue marker command only when we are going
7260                                  * to start the I/O .
7261                                  */
7262                                 vha->marker_needed = 1;
7263                         }
7264
7265                         vha->flags.online = 1;
7266
7267                         ha->isp_ops->enable_intrs(ha);
7268
7269                         ha->isp_abort_cnt = 0;
7270                         clear_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
7271
7272                         if (IS_QLA81XX(ha) || IS_QLA8031(ha))
7273                                 qla2x00_get_fw_version(vha);
7274                         if (ha->fce) {
7275                                 ha->flags.fce_enabled = 1;
7276                                 memset(ha->fce, 0,
7277                                     fce_calc_size(ha->fce_bufs));
7278                                 rval = qla2x00_enable_fce_trace(vha,
7279                                     ha->fce_dma, ha->fce_bufs, ha->fce_mb,
7280                                     &ha->fce_bufs);
7281                                 if (rval) {
7282                                         ql_log(ql_log_warn, vha, 0x8033,
7283                                             "Unable to reinitialize FCE "
7284                                             "(%d).\n", rval);
7285                                         ha->flags.fce_enabled = 0;
7286                                 }
7287                         }
7288
7289                         if (ha->eft) {
7290                                 memset(ha->eft, 0, EFT_SIZE);
7291                                 rval = qla2x00_enable_eft_trace(vha,
7292                                     ha->eft_dma, EFT_NUM_BUFFERS);
7293                                 if (rval) {
7294                                         ql_log(ql_log_warn, vha, 0x8034,
7295                                             "Unable to reinitialize EFT "
7296                                             "(%d).\n", rval);
7297                                 }
7298                         }
7299                 } else {        /* failed the ISP abort */
7300                         vha->flags.online = 1;
7301                         if (test_bit(ISP_ABORT_RETRY, &vha->dpc_flags)) {
7302                                 if (ha->isp_abort_cnt == 0) {
7303                                         ql_log(ql_log_fatal, vha, 0x8035,
7304                                             "ISP error recover failed - "
7305                                             "board disabled.\n");
7306                                         /*
7307                                          * The next call disables the board
7308                                          * completely.
7309                                          */
7310                                         qla2x00_abort_isp_cleanup(vha);
7311                                         vha->flags.online = 0;
7312                                         clear_bit(ISP_ABORT_RETRY,
7313                                             &vha->dpc_flags);
7314                                         status = 0;
7315                                 } else { /* schedule another ISP abort */
7316                                         ha->isp_abort_cnt--;
7317                                         ql_dbg(ql_dbg_taskm, vha, 0x8020,
7318                                             "ISP abort - retry remaining %d.\n",
7319                                             ha->isp_abort_cnt);
7320                                         status = 1;
7321                                 }
7322                         } else {
7323                                 ha->isp_abort_cnt = MAX_RETRIES_OF_ISP_ABORT;
7324                                 ql_dbg(ql_dbg_taskm, vha, 0x8021,
7325                                     "ISP error recovery - retrying (%d) "
7326                                     "more times.\n", ha->isp_abort_cnt);
7327                                 set_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
7328                                 status = 1;
7329                         }
7330                 }
7331
7332         }
7333
7334         if (vha->hw->flags.port_isolated) {
7335                 qla2x00_abort_isp_cleanup(vha);
7336                 return status;
7337         }
7338
7339         if (!status) {
7340                 ql_dbg(ql_dbg_taskm, vha, 0x8022, "%s succeeded.\n", __func__);
7341                 qla2x00_configure_hba(vha);
7342                 spin_lock_irqsave(&ha->vport_slock, flags);
7343                 list_for_each_entry_safe(vp, tvp, &ha->vp_list, list) {
7344                         if (vp->vp_idx) {
7345                                 atomic_inc(&vp->vref_count);
7346                                 spin_unlock_irqrestore(&ha->vport_slock, flags);
7347
7348                                 qla2x00_vp_abort_isp(vp);
7349
7350                                 spin_lock_irqsave(&ha->vport_slock, flags);
7351                                 atomic_dec(&vp->vref_count);
7352                         }
7353                 }
7354                 spin_unlock_irqrestore(&ha->vport_slock, flags);
7355
7356                 if (IS_QLA8031(ha)) {
7357                         ql_dbg(ql_dbg_p3p, vha, 0xb05d,
7358                             "Setting back fcoe driver presence.\n");
7359                         if (qla83xx_set_drv_presence(vha) != QLA_SUCCESS)
7360                                 ql_dbg(ql_dbg_p3p, vha, 0xb074,
7361                                     "Error while setting DRV-Presence.\n");
7362                 }
7363         } else {
7364                 ql_log(ql_log_warn, vha, 0x8023, "%s **** FAILED ****.\n",
7365                        __func__);
7366         }
7367
7368         return(status);
7369 }
7370
7371 /*
7372 *  qla2x00_restart_isp
7373 *      restarts the ISP after a reset
7374 *
7375 * Input:
7376 *      ha = adapter block pointer.
7377 *
7378 * Returns:
7379 *      0 = success
7380 */
7381 static int
7382 qla2x00_restart_isp(scsi_qla_host_t *vha)
7383 {
7384         int status;
7385         struct qla_hw_data *ha = vha->hw;
7386
7387         /* If firmware needs to be loaded */
7388         if (qla2x00_isp_firmware(vha)) {
7389                 vha->flags.online = 0;
7390                 status = ha->isp_ops->chip_diag(vha);
7391                 if (status)
7392                         return status;
7393                 status = qla2x00_setup_chip(vha);
7394                 if (status)
7395                         return status;
7396         }
7397
7398         status = qla2x00_init_rings(vha);
7399         if (status)
7400                 return status;
7401
7402         clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
7403         ha->flags.chip_reset_done = 1;
7404
7405         /* Initialize the queues in use */
7406         qla25xx_init_queues(ha);
7407
7408         status = qla2x00_fw_ready(vha);
7409         if (status) {
7410                 /* if no cable then assume it's good */
7411                 return vha->device_flags & DFLG_NO_CABLE ? 0 : status;
7412         }
7413
7414         /* Issue a marker after FW becomes ready. */
7415         qla2x00_marker(vha, ha->base_qpair, 0, 0, MK_SYNC_ALL);
7416         set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
7417
7418         return 0;
7419 }
7420
7421 static int
7422 qla25xx_init_queues(struct qla_hw_data *ha)
7423 {
7424         struct rsp_que *rsp = NULL;
7425         struct req_que *req = NULL;
7426         struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
7427         int ret = -1;
7428         int i;
7429
7430         for (i = 1; i < ha->max_rsp_queues; i++) {
7431                 rsp = ha->rsp_q_map[i];
7432                 if (rsp && test_bit(i, ha->rsp_qid_map)) {
7433                         rsp->options &= ~BIT_0;
7434                         ret = qla25xx_init_rsp_que(base_vha, rsp);
7435                         if (ret != QLA_SUCCESS)
7436                                 ql_dbg(ql_dbg_init, base_vha, 0x00ff,
7437                                     "%s Rsp que: %d init failed.\n",
7438                                     __func__, rsp->id);
7439                         else
7440                                 ql_dbg(ql_dbg_init, base_vha, 0x0100,
7441                                     "%s Rsp que: %d inited.\n",
7442                                     __func__, rsp->id);
7443                 }
7444         }
7445         for (i = 1; i < ha->max_req_queues; i++) {
7446                 req = ha->req_q_map[i];
7447                 if (req && test_bit(i, ha->req_qid_map)) {
7448                         /* Clear outstanding commands array. */
7449                         req->options &= ~BIT_0;
7450                         ret = qla25xx_init_req_que(base_vha, req);
7451                         if (ret != QLA_SUCCESS)
7452                                 ql_dbg(ql_dbg_init, base_vha, 0x0101,
7453                                     "%s Req que: %d init failed.\n",
7454                                     __func__, req->id);
7455                         else
7456                                 ql_dbg(ql_dbg_init, base_vha, 0x0102,
7457                                     "%s Req que: %d inited.\n",
7458                                     __func__, req->id);
7459                 }
7460         }
7461         return ret;
7462 }
7463
7464 /*
7465 * qla2x00_reset_adapter
7466 *      Reset adapter.
7467 *
7468 * Input:
7469 *      ha = adapter block pointer.
7470 */
7471 int
7472 qla2x00_reset_adapter(scsi_qla_host_t *vha)
7473 {
7474         unsigned long flags = 0;
7475         struct qla_hw_data *ha = vha->hw;
7476         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
7477
7478         vha->flags.online = 0;
7479         ha->isp_ops->disable_intrs(ha);
7480
7481         spin_lock_irqsave(&ha->hardware_lock, flags);
7482         wrt_reg_word(&reg->hccr, HCCR_RESET_RISC);
7483         rd_reg_word(&reg->hccr);                        /* PCI Posting. */
7484         wrt_reg_word(&reg->hccr, HCCR_RELEASE_RISC);
7485         rd_reg_word(&reg->hccr);                        /* PCI Posting. */
7486         spin_unlock_irqrestore(&ha->hardware_lock, flags);
7487
7488         return QLA_SUCCESS;
7489 }
7490
7491 int
7492 qla24xx_reset_adapter(scsi_qla_host_t *vha)
7493 {
7494         unsigned long flags = 0;
7495         struct qla_hw_data *ha = vha->hw;
7496         struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
7497
7498         if (IS_P3P_TYPE(ha))
7499                 return QLA_SUCCESS;
7500
7501         vha->flags.online = 0;
7502         ha->isp_ops->disable_intrs(ha);
7503
7504         spin_lock_irqsave(&ha->hardware_lock, flags);
7505         wrt_reg_dword(&reg->hccr, HCCRX_SET_RISC_RESET);
7506         rd_reg_dword(&reg->hccr);
7507         wrt_reg_dword(&reg->hccr, HCCRX_REL_RISC_PAUSE);
7508         rd_reg_dword(&reg->hccr);
7509         spin_unlock_irqrestore(&ha->hardware_lock, flags);
7510
7511         if (IS_NOPOLLING_TYPE(ha))
7512                 ha->isp_ops->enable_intrs(ha);
7513
7514         return QLA_SUCCESS;
7515 }
7516
7517 /* On sparc systems, obtain port and node WWN from firmware
7518  * properties.
7519  */
7520 static void qla24xx_nvram_wwn_from_ofw(scsi_qla_host_t *vha,
7521         struct nvram_24xx *nv)
7522 {
7523 #ifdef CONFIG_SPARC
7524         struct qla_hw_data *ha = vha->hw;
7525         struct pci_dev *pdev = ha->pdev;
7526         struct device_node *dp = pci_device_to_OF_node(pdev);
7527         const u8 *val;
7528         int len;
7529
7530         val = of_get_property(dp, "port-wwn", &len);
7531         if (val && len >= WWN_SIZE)
7532                 memcpy(nv->port_name, val, WWN_SIZE);
7533
7534         val = of_get_property(dp, "node-wwn", &len);
7535         if (val && len >= WWN_SIZE)
7536                 memcpy(nv->node_name, val, WWN_SIZE);
7537 #endif
7538 }
7539
7540 int
7541 qla24xx_nvram_config(scsi_qla_host_t *vha)
7542 {
7543         int   rval;
7544         struct init_cb_24xx *icb;
7545         struct nvram_24xx *nv;
7546         __le32 *dptr;
7547         uint8_t  *dptr1, *dptr2;
7548         uint32_t chksum;
7549         uint16_t cnt;
7550         struct qla_hw_data *ha = vha->hw;
7551
7552         rval = QLA_SUCCESS;
7553         icb = (struct init_cb_24xx *)ha->init_cb;
7554         nv = ha->nvram;
7555
7556         /* Determine NVRAM starting address. */
7557         if (ha->port_no == 0) {
7558                 ha->nvram_base = FA_NVRAM_FUNC0_ADDR;
7559                 ha->vpd_base = FA_NVRAM_VPD0_ADDR;
7560         } else {
7561                 ha->nvram_base = FA_NVRAM_FUNC1_ADDR;
7562                 ha->vpd_base = FA_NVRAM_VPD1_ADDR;
7563         }
7564
7565         ha->nvram_size = sizeof(*nv);
7566         ha->vpd_size = FA_NVRAM_VPD_SIZE;
7567
7568         /* Get VPD data into cache */
7569         ha->vpd = ha->nvram + VPD_OFFSET;
7570         ha->isp_ops->read_nvram(vha, ha->vpd,
7571             ha->nvram_base - FA_NVRAM_FUNC0_ADDR, FA_NVRAM_VPD_SIZE * 4);
7572
7573         /* Get NVRAM data into cache and calculate checksum. */
7574         dptr = (__force __le32 *)nv;
7575         ha->isp_ops->read_nvram(vha, dptr, ha->nvram_base, ha->nvram_size);
7576         for (cnt = 0, chksum = 0; cnt < ha->nvram_size >> 2; cnt++, dptr++)
7577                 chksum += le32_to_cpu(*dptr);
7578
7579         ql_dbg(ql_dbg_init + ql_dbg_buffer, vha, 0x006a,
7580             "Contents of NVRAM\n");
7581         ql_dump_buffer(ql_dbg_init + ql_dbg_buffer, vha, 0x010d,
7582             nv, ha->nvram_size);
7583
7584         /* Bad NVRAM data, set defaults parameters. */
7585         if (chksum || memcmp("ISP ", nv->id, sizeof(nv->id)) ||
7586             le16_to_cpu(nv->nvram_version) < ICB_VERSION) {
7587                 /* Reset NVRAM data. */
7588                 ql_log(ql_log_warn, vha, 0x006b,
7589                     "Inconsistent NVRAM checksum=%#x id=%.4s version=%#x.\n",
7590                     chksum, nv->id, nv->nvram_version);
7591                 ql_dump_buffer(ql_dbg_init, vha, 0x006b, nv, sizeof(*nv));
7592                 ql_log(ql_log_warn, vha, 0x006c,
7593                     "Falling back to functioning (yet invalid -- WWPN) "
7594                     "defaults.\n");
7595
7596                 /*
7597                  * Set default initialization control block.
7598                  */
7599                 memset(nv, 0, ha->nvram_size);
7600                 nv->nvram_version = cpu_to_le16(ICB_VERSION);
7601                 nv->version = cpu_to_le16(ICB_VERSION);
7602                 nv->frame_payload_size = cpu_to_le16(2048);
7603                 nv->execution_throttle = cpu_to_le16(0xFFFF);
7604                 nv->exchange_count = cpu_to_le16(0);
7605                 nv->hard_address = cpu_to_le16(124);
7606                 nv->port_name[0] = 0x21;
7607                 nv->port_name[1] = 0x00 + ha->port_no + 1;
7608                 nv->port_name[2] = 0x00;
7609                 nv->port_name[3] = 0xe0;
7610                 nv->port_name[4] = 0x8b;
7611                 nv->port_name[5] = 0x1c;
7612                 nv->port_name[6] = 0x55;
7613                 nv->port_name[7] = 0x86;
7614                 nv->node_name[0] = 0x20;
7615                 nv->node_name[1] = 0x00;
7616                 nv->node_name[2] = 0x00;
7617                 nv->node_name[3] = 0xe0;
7618                 nv->node_name[4] = 0x8b;
7619                 nv->node_name[5] = 0x1c;
7620                 nv->node_name[6] = 0x55;
7621                 nv->node_name[7] = 0x86;
7622                 qla24xx_nvram_wwn_from_ofw(vha, nv);
7623                 nv->login_retry_count = cpu_to_le16(8);
7624                 nv->interrupt_delay_timer = cpu_to_le16(0);
7625                 nv->login_timeout = cpu_to_le16(0);
7626                 nv->firmware_options_1 =
7627                     cpu_to_le32(BIT_14|BIT_13|BIT_2|BIT_1);
7628                 nv->firmware_options_2 = cpu_to_le32(2 << 4);
7629                 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
7630                 nv->firmware_options_3 = cpu_to_le32(2 << 13);
7631                 nv->host_p = cpu_to_le32(BIT_11|BIT_10);
7632                 nv->efi_parameters = cpu_to_le32(0);
7633                 nv->reset_delay = 5;
7634                 nv->max_luns_per_target = cpu_to_le16(128);
7635                 nv->port_down_retry_count = cpu_to_le16(30);
7636                 nv->link_down_timeout = cpu_to_le16(30);
7637
7638                 rval = 1;
7639         }
7640
7641         if (qla_tgt_mode_enabled(vha)) {
7642                 /* Don't enable full login after initial LIP */
7643                 nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
7644                 /* Don't enable LIP full login for initiator */
7645                 nv->host_p &= cpu_to_le32(~BIT_10);
7646         }
7647
7648         qlt_24xx_config_nvram_stage1(vha, nv);
7649
7650         /* Reset Initialization control block */
7651         memset(icb, 0, ha->init_cb_size);
7652
7653         /* Copy 1st segment. */
7654         dptr1 = (uint8_t *)icb;
7655         dptr2 = (uint8_t *)&nv->version;
7656         cnt = (uint8_t *)&icb->response_q_inpointer - (uint8_t *)&icb->version;
7657         while (cnt--)
7658                 *dptr1++ = *dptr2++;
7659
7660         icb->login_retry_count = nv->login_retry_count;
7661         icb->link_down_on_nos = nv->link_down_on_nos;
7662
7663         /* Copy 2nd segment. */
7664         dptr1 = (uint8_t *)&icb->interrupt_delay_timer;
7665         dptr2 = (uint8_t *)&nv->interrupt_delay_timer;
7666         cnt = (uint8_t *)&icb->reserved_3 -
7667             (uint8_t *)&icb->interrupt_delay_timer;
7668         while (cnt--)
7669                 *dptr1++ = *dptr2++;
7670         ha->frame_payload_size = le16_to_cpu(icb->frame_payload_size);
7671         /*
7672          * Setup driver NVRAM options.
7673          */
7674         qla2x00_set_model_info(vha, nv->model_name, sizeof(nv->model_name),
7675             "QLA2462");
7676
7677         qlt_24xx_config_nvram_stage2(vha, icb);
7678
7679         if (nv->host_p & cpu_to_le32(BIT_15)) {
7680                 /* Use alternate WWN? */
7681                 memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
7682                 memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
7683         }
7684
7685         /* Prepare nodename */
7686         if ((icb->firmware_options_1 & cpu_to_le32(BIT_14)) == 0) {
7687                 /*
7688                  * Firmware will apply the following mask if the nodename was
7689                  * not provided.
7690                  */
7691                 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
7692                 icb->node_name[0] &= 0xF0;
7693         }
7694
7695         /* Set host adapter parameters. */
7696         ha->flags.disable_risc_code_load = 0;
7697         ha->flags.enable_lip_reset = 0;
7698         ha->flags.enable_lip_full_login =
7699             le32_to_cpu(nv->host_p) & BIT_10 ? 1 : 0;
7700         ha->flags.enable_target_reset =
7701             le32_to_cpu(nv->host_p) & BIT_11 ? 1 : 0;
7702         ha->flags.enable_led_scheme = 0;
7703         ha->flags.disable_serdes = le32_to_cpu(nv->host_p) & BIT_5 ? 1 : 0;
7704
7705         ha->operating_mode = (le32_to_cpu(icb->firmware_options_2) &
7706             (BIT_6 | BIT_5 | BIT_4)) >> 4;
7707
7708         memcpy(ha->fw_seriallink_options24, nv->seriallink_options,
7709             sizeof(ha->fw_seriallink_options24));
7710
7711         /* save HBA serial number */
7712         ha->serial0 = icb->port_name[5];
7713         ha->serial1 = icb->port_name[6];
7714         ha->serial2 = icb->port_name[7];
7715         memcpy(vha->node_name, icb->node_name, WWN_SIZE);
7716         memcpy(vha->port_name, icb->port_name, WWN_SIZE);
7717
7718         icb->execution_throttle = cpu_to_le16(0xFFFF);
7719
7720         ha->retry_count = le16_to_cpu(nv->login_retry_count);
7721
7722         /* Set minimum login_timeout to 4 seconds. */
7723         if (le16_to_cpu(nv->login_timeout) < ql2xlogintimeout)
7724                 nv->login_timeout = cpu_to_le16(ql2xlogintimeout);
7725         if (le16_to_cpu(nv->login_timeout) < 4)
7726                 nv->login_timeout = cpu_to_le16(4);
7727         ha->login_timeout = le16_to_cpu(nv->login_timeout);
7728
7729         /* Set minimum RATOV to 100 tenths of a second. */
7730         ha->r_a_tov = 100;
7731
7732         ha->loop_reset_delay = nv->reset_delay;
7733
7734         /* Link Down Timeout = 0:
7735          *
7736          *      When Port Down timer expires we will start returning
7737          *      I/O's to OS with "DID_NO_CONNECT".
7738          *
7739          * Link Down Timeout != 0:
7740          *
7741          *       The driver waits for the link to come up after link down
7742          *       before returning I/Os to OS with "DID_NO_CONNECT".
7743          */
7744         if (le16_to_cpu(nv->link_down_timeout) == 0) {
7745                 ha->loop_down_abort_time =
7746                     (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
7747         } else {
7748                 ha->link_down_timeout = le16_to_cpu(nv->link_down_timeout);
7749                 ha->loop_down_abort_time =
7750                     (LOOP_DOWN_TIME - ha->link_down_timeout);
7751         }
7752
7753         /* Need enough time to try and get the port back. */
7754         ha->port_down_retry_count = le16_to_cpu(nv->port_down_retry_count);
7755         if (qlport_down_retry)
7756                 ha->port_down_retry_count = qlport_down_retry;
7757
7758         /* Set login_retry_count */
7759         ha->login_retry_count  = le16_to_cpu(nv->login_retry_count);
7760         if (ha->port_down_retry_count ==
7761             le16_to_cpu(nv->port_down_retry_count) &&
7762             ha->port_down_retry_count > 3)
7763                 ha->login_retry_count = ha->port_down_retry_count;
7764         else if (ha->port_down_retry_count > (int)ha->login_retry_count)
7765                 ha->login_retry_count = ha->port_down_retry_count;
7766         if (ql2xloginretrycount)
7767                 ha->login_retry_count = ql2xloginretrycount;
7768
7769         /* N2N: driver will initiate Login instead of FW */
7770         icb->firmware_options_3 |= cpu_to_le32(BIT_8);
7771
7772         /* Enable ZIO. */
7773         if (!vha->flags.init_done) {
7774                 ha->zio_mode = le32_to_cpu(icb->firmware_options_2) &
7775                     (BIT_3 | BIT_2 | BIT_1 | BIT_0);
7776                 ha->zio_timer = le16_to_cpu(icb->interrupt_delay_timer) ?
7777                     le16_to_cpu(icb->interrupt_delay_timer) : 2;
7778         }
7779         icb->firmware_options_2 &= cpu_to_le32(
7780             ~(BIT_3 | BIT_2 | BIT_1 | BIT_0));
7781         if (ha->zio_mode != QLA_ZIO_DISABLED) {
7782                 ha->zio_mode = QLA_ZIO_MODE_6;
7783
7784                 ql_log(ql_log_info, vha, 0x006f,
7785                     "ZIO mode %d enabled; timer delay (%d us).\n",
7786                     ha->zio_mode, ha->zio_timer * 100);
7787
7788                 icb->firmware_options_2 |= cpu_to_le32(
7789                     (uint32_t)ha->zio_mode);
7790                 icb->interrupt_delay_timer = cpu_to_le16(ha->zio_timer);
7791         }
7792
7793         if (rval) {
7794                 ql_log(ql_log_warn, vha, 0x0070,
7795                     "NVRAM configuration failed.\n");
7796         }
7797         return (rval);
7798 }
7799
7800 static void
7801 qla27xx_print_image(struct scsi_qla_host *vha, char *name,
7802     struct qla27xx_image_status *image_status)
7803 {
7804         ql_dbg(ql_dbg_init, vha, 0x018b,
7805             "%s %s: mask=%#02x gen=%#04x ver=%u.%u map=%#01x sum=%#08x sig=%#08x\n",
7806             name, "status",
7807             image_status->image_status_mask,
7808             le16_to_cpu(image_status->generation),
7809             image_status->ver_major,
7810             image_status->ver_minor,
7811             image_status->bitmap,
7812             le32_to_cpu(image_status->checksum),
7813             le32_to_cpu(image_status->signature));
7814 }
7815
7816 static bool
7817 qla28xx_check_aux_image_status_signature(
7818     struct qla27xx_image_status *image_status)
7819 {
7820         ulong signature = le32_to_cpu(image_status->signature);
7821
7822         return signature != QLA28XX_AUX_IMG_STATUS_SIGN;
7823 }
7824
7825 static bool
7826 qla27xx_check_image_status_signature(struct qla27xx_image_status *image_status)
7827 {
7828         ulong signature = le32_to_cpu(image_status->signature);
7829
7830         return
7831             signature != QLA27XX_IMG_STATUS_SIGN &&
7832             signature != QLA28XX_IMG_STATUS_SIGN;
7833 }
7834
7835 static ulong
7836 qla27xx_image_status_checksum(struct qla27xx_image_status *image_status)
7837 {
7838         __le32 *p = (__force __le32 *)image_status;
7839         uint n = sizeof(*image_status) / sizeof(*p);
7840         uint32_t sum = 0;
7841
7842         for ( ; n--; p++)
7843                 sum += le32_to_cpup(p);
7844
7845         return sum;
7846 }
7847
7848 static inline uint
7849 qla28xx_component_bitmask(struct qla27xx_image_status *aux, uint bitmask)
7850 {
7851         return aux->bitmap & bitmask ?
7852             QLA27XX_SECONDARY_IMAGE : QLA27XX_PRIMARY_IMAGE;
7853 }
7854
7855 static void
7856 qla28xx_component_status(
7857     struct active_regions *active_regions, struct qla27xx_image_status *aux)
7858 {
7859         active_regions->aux.board_config =
7860             qla28xx_component_bitmask(aux, QLA28XX_AUX_IMG_BOARD_CONFIG);
7861
7862         active_regions->aux.vpd_nvram =
7863             qla28xx_component_bitmask(aux, QLA28XX_AUX_IMG_VPD_NVRAM);
7864
7865         active_regions->aux.npiv_config_0_1 =
7866             qla28xx_component_bitmask(aux, QLA28XX_AUX_IMG_NPIV_CONFIG_0_1);
7867
7868         active_regions->aux.npiv_config_2_3 =
7869             qla28xx_component_bitmask(aux, QLA28XX_AUX_IMG_NPIV_CONFIG_2_3);
7870 }
7871
7872 static int
7873 qla27xx_compare_image_generation(
7874     struct qla27xx_image_status *pri_image_status,
7875     struct qla27xx_image_status *sec_image_status)
7876 {
7877         /* calculate generation delta as uint16 (this accounts for wrap) */
7878         int16_t delta =
7879             le16_to_cpu(pri_image_status->generation) -
7880             le16_to_cpu(sec_image_status->generation);
7881
7882         ql_dbg(ql_dbg_init, NULL, 0x0180, "generation delta = %d\n", delta);
7883
7884         return delta;
7885 }
7886
7887 void
7888 qla28xx_get_aux_images(
7889         struct scsi_qla_host *vha, struct active_regions *active_regions)
7890 {
7891         struct qla_hw_data *ha = vha->hw;
7892         struct qla27xx_image_status pri_aux_image_status, sec_aux_image_status;
7893         bool valid_pri_image = false, valid_sec_image = false;
7894         bool active_pri_image = false, active_sec_image = false;
7895
7896         if (!ha->flt_region_aux_img_status_pri) {
7897                 ql_dbg(ql_dbg_init, vha, 0x018a, "Primary aux image not addressed\n");
7898                 goto check_sec_image;
7899         }
7900
7901         qla24xx_read_flash_data(vha, (uint32_t *)&pri_aux_image_status,
7902             ha->flt_region_aux_img_status_pri,
7903             sizeof(pri_aux_image_status) >> 2);
7904         qla27xx_print_image(vha, "Primary aux image", &pri_aux_image_status);
7905
7906         if (qla28xx_check_aux_image_status_signature(&pri_aux_image_status)) {
7907                 ql_dbg(ql_dbg_init, vha, 0x018b,
7908                     "Primary aux image signature (%#x) not valid\n",
7909                     le32_to_cpu(pri_aux_image_status.signature));
7910                 goto check_sec_image;
7911         }
7912
7913         if (qla27xx_image_status_checksum(&pri_aux_image_status)) {
7914                 ql_dbg(ql_dbg_init, vha, 0x018c,
7915                     "Primary aux image checksum failed\n");
7916                 goto check_sec_image;
7917         }
7918
7919         valid_pri_image = true;
7920
7921         if (pri_aux_image_status.image_status_mask & 1) {
7922                 ql_dbg(ql_dbg_init, vha, 0x018d,
7923                     "Primary aux image is active\n");
7924                 active_pri_image = true;
7925         }
7926
7927 check_sec_image:
7928         if (!ha->flt_region_aux_img_status_sec) {
7929                 ql_dbg(ql_dbg_init, vha, 0x018a,
7930                     "Secondary aux image not addressed\n");
7931                 goto check_valid_image;
7932         }
7933
7934         qla24xx_read_flash_data(vha, (uint32_t *)&sec_aux_image_status,
7935             ha->flt_region_aux_img_status_sec,
7936             sizeof(sec_aux_image_status) >> 2);
7937         qla27xx_print_image(vha, "Secondary aux image", &sec_aux_image_status);
7938
7939         if (qla28xx_check_aux_image_status_signature(&sec_aux_image_status)) {
7940                 ql_dbg(ql_dbg_init, vha, 0x018b,
7941                     "Secondary aux image signature (%#x) not valid\n",
7942                     le32_to_cpu(sec_aux_image_status.signature));
7943                 goto check_valid_image;
7944         }
7945
7946         if (qla27xx_image_status_checksum(&sec_aux_image_status)) {
7947                 ql_dbg(ql_dbg_init, vha, 0x018c,
7948                     "Secondary aux image checksum failed\n");
7949                 goto check_valid_image;
7950         }
7951
7952         valid_sec_image = true;
7953
7954         if (sec_aux_image_status.image_status_mask & 1) {
7955                 ql_dbg(ql_dbg_init, vha, 0x018d,
7956                     "Secondary aux image is active\n");
7957                 active_sec_image = true;
7958         }
7959
7960 check_valid_image:
7961         if (valid_pri_image && active_pri_image &&
7962             valid_sec_image && active_sec_image) {
7963                 if (qla27xx_compare_image_generation(&pri_aux_image_status,
7964                     &sec_aux_image_status) >= 0) {
7965                         qla28xx_component_status(active_regions,
7966                             &pri_aux_image_status);
7967                 } else {
7968                         qla28xx_component_status(active_regions,
7969                             &sec_aux_image_status);
7970                 }
7971         } else if (valid_pri_image && active_pri_image) {
7972                 qla28xx_component_status(active_regions, &pri_aux_image_status);
7973         } else if (valid_sec_image && active_sec_image) {
7974                 qla28xx_component_status(active_regions, &sec_aux_image_status);
7975         }
7976
7977         ql_dbg(ql_dbg_init, vha, 0x018f,
7978             "aux images active: BCFG=%u VPD/NVR=%u NPIV0/1=%u NPIV2/3=%u\n",
7979             active_regions->aux.board_config,
7980             active_regions->aux.vpd_nvram,
7981             active_regions->aux.npiv_config_0_1,
7982             active_regions->aux.npiv_config_2_3);
7983 }
7984
7985 void
7986 qla27xx_get_active_image(struct scsi_qla_host *vha,
7987     struct active_regions *active_regions)
7988 {
7989         struct qla_hw_data *ha = vha->hw;
7990         struct qla27xx_image_status pri_image_status, sec_image_status;
7991         bool valid_pri_image = false, valid_sec_image = false;
7992         bool active_pri_image = false, active_sec_image = false;
7993
7994         if (!ha->flt_region_img_status_pri) {
7995                 ql_dbg(ql_dbg_init, vha, 0x018a, "Primary image not addressed\n");
7996                 goto check_sec_image;
7997         }
7998
7999         if (qla24xx_read_flash_data(vha, (uint32_t *)&pri_image_status,
8000             ha->flt_region_img_status_pri, sizeof(pri_image_status) >> 2) !=
8001             QLA_SUCCESS) {
8002                 WARN_ON_ONCE(true);
8003                 goto check_sec_image;
8004         }
8005         qla27xx_print_image(vha, "Primary image", &pri_image_status);
8006
8007         if (qla27xx_check_image_status_signature(&pri_image_status)) {
8008                 ql_dbg(ql_dbg_init, vha, 0x018b,
8009                     "Primary image signature (%#x) not valid\n",
8010                     le32_to_cpu(pri_image_status.signature));
8011                 goto check_sec_image;
8012         }
8013
8014         if (qla27xx_image_status_checksum(&pri_image_status)) {
8015                 ql_dbg(ql_dbg_init, vha, 0x018c,
8016                     "Primary image checksum failed\n");
8017                 goto check_sec_image;
8018         }
8019
8020         valid_pri_image = true;
8021
8022         if (pri_image_status.image_status_mask & 1) {
8023                 ql_dbg(ql_dbg_init, vha, 0x018d,
8024                     "Primary image is active\n");
8025                 active_pri_image = true;
8026         }
8027
8028 check_sec_image:
8029         if (!ha->flt_region_img_status_sec) {
8030                 ql_dbg(ql_dbg_init, vha, 0x018a, "Secondary image not addressed\n");
8031                 goto check_valid_image;
8032         }
8033
8034         qla24xx_read_flash_data(vha, (uint32_t *)(&sec_image_status),
8035             ha->flt_region_img_status_sec, sizeof(sec_image_status) >> 2);
8036         qla27xx_print_image(vha, "Secondary image", &sec_image_status);
8037
8038         if (qla27xx_check_image_status_signature(&sec_image_status)) {
8039                 ql_dbg(ql_dbg_init, vha, 0x018b,
8040                     "Secondary image signature (%#x) not valid\n",
8041                     le32_to_cpu(sec_image_status.signature));
8042                 goto check_valid_image;
8043         }
8044
8045         if (qla27xx_image_status_checksum(&sec_image_status)) {
8046                 ql_dbg(ql_dbg_init, vha, 0x018c,
8047                     "Secondary image checksum failed\n");
8048                 goto check_valid_image;
8049         }
8050
8051         valid_sec_image = true;
8052
8053         if (sec_image_status.image_status_mask & 1) {
8054                 ql_dbg(ql_dbg_init, vha, 0x018d,
8055                     "Secondary image is active\n");
8056                 active_sec_image = true;
8057         }
8058
8059 check_valid_image:
8060         if (valid_pri_image && active_pri_image)
8061                 active_regions->global = QLA27XX_PRIMARY_IMAGE;
8062
8063         if (valid_sec_image && active_sec_image) {
8064                 if (!active_regions->global ||
8065                     qla27xx_compare_image_generation(
8066                         &pri_image_status, &sec_image_status) < 0) {
8067                         active_regions->global = QLA27XX_SECONDARY_IMAGE;
8068                 }
8069         }
8070
8071         ql_dbg(ql_dbg_init, vha, 0x018f, "active image %s (%u)\n",
8072             active_regions->global == QLA27XX_DEFAULT_IMAGE ?
8073                 "default (boot/fw)" :
8074             active_regions->global == QLA27XX_PRIMARY_IMAGE ?
8075                 "primary" :
8076             active_regions->global == QLA27XX_SECONDARY_IMAGE ?
8077                 "secondary" : "invalid",
8078             active_regions->global);
8079 }
8080
8081 bool qla24xx_risc_firmware_invalid(uint32_t *dword)
8082 {
8083         return
8084             !(dword[4] | dword[5] | dword[6] | dword[7]) ||
8085             !(~dword[4] | ~dword[5] | ~dword[6] | ~dword[7]);
8086 }
8087
8088 static int
8089 qla24xx_load_risc_flash(scsi_qla_host_t *vha, uint32_t *srisc_addr,
8090     uint32_t faddr)
8091 {
8092         int rval;
8093         uint templates, segments, fragment;
8094         ulong i;
8095         uint j;
8096         ulong dlen;
8097         uint32_t *dcode;
8098         uint32_t risc_addr, risc_size, risc_attr = 0;
8099         struct qla_hw_data *ha = vha->hw;
8100         struct req_que *req = ha->req_q_map[0];
8101         struct fwdt *fwdt = ha->fwdt;
8102
8103         ql_dbg(ql_dbg_init, vha, 0x008b,
8104             "FW: Loading firmware from flash (%x).\n", faddr);
8105
8106         dcode = (uint32_t *)req->ring;
8107         qla24xx_read_flash_data(vha, dcode, faddr, 8);
8108         if (qla24xx_risc_firmware_invalid(dcode)) {
8109                 ql_log(ql_log_fatal, vha, 0x008c,
8110                     "Unable to verify the integrity of flash firmware "
8111                     "image.\n");
8112                 ql_log(ql_log_fatal, vha, 0x008d,
8113                     "Firmware data: %08x %08x %08x %08x.\n",
8114                     dcode[0], dcode[1], dcode[2], dcode[3]);
8115
8116                 return QLA_FUNCTION_FAILED;
8117         }
8118
8119         dcode = (uint32_t *)req->ring;
8120         *srisc_addr = 0;
8121         segments = FA_RISC_CODE_SEGMENTS;
8122         for (j = 0; j < segments; j++) {
8123                 ql_dbg(ql_dbg_init, vha, 0x008d,
8124                     "-> Loading segment %u...\n", j);
8125                 qla24xx_read_flash_data(vha, dcode, faddr, 10);
8126                 risc_addr = be32_to_cpu((__force __be32)dcode[2]);
8127                 risc_size = be32_to_cpu((__force __be32)dcode[3]);
8128                 if (!*srisc_addr) {
8129                         *srisc_addr = risc_addr;
8130                         risc_attr = be32_to_cpu((__force __be32)dcode[9]);
8131                 }
8132
8133                 dlen = ha->fw_transfer_size >> 2;
8134                 for (fragment = 0; risc_size; fragment++) {
8135                         if (dlen > risc_size)
8136                                 dlen = risc_size;
8137
8138                         ql_dbg(ql_dbg_init, vha, 0x008e,
8139                             "-> Loading fragment %u: %#x <- %#x (%#lx dwords)...\n",
8140                             fragment, risc_addr, faddr, dlen);
8141                         qla24xx_read_flash_data(vha, dcode, faddr, dlen);
8142                         for (i = 0; i < dlen; i++)
8143                                 dcode[i] = swab32(dcode[i]);
8144
8145                         rval = qla2x00_load_ram(vha, req->dma, risc_addr, dlen);
8146                         if (rval) {
8147                                 ql_log(ql_log_fatal, vha, 0x008f,
8148                                     "-> Failed load firmware fragment %u.\n",
8149                                     fragment);
8150                                 return QLA_FUNCTION_FAILED;
8151                         }
8152
8153                         faddr += dlen;
8154                         risc_addr += dlen;
8155                         risc_size -= dlen;
8156                 }
8157         }
8158
8159         if (!IS_QLA27XX(ha) && !IS_QLA28XX(ha))
8160                 return QLA_SUCCESS;
8161
8162         templates = (risc_attr & BIT_9) ? 2 : 1;
8163         ql_dbg(ql_dbg_init, vha, 0x0160, "-> templates = %u\n", templates);
8164         for (j = 0; j < templates; j++, fwdt++) {
8165                 vfree(fwdt->template);
8166                 fwdt->template = NULL;
8167                 fwdt->length = 0;
8168
8169                 dcode = (uint32_t *)req->ring;
8170                 qla24xx_read_flash_data(vha, dcode, faddr, 7);
8171                 risc_size = be32_to_cpu((__force __be32)dcode[2]);
8172                 ql_dbg(ql_dbg_init, vha, 0x0161,
8173                     "-> fwdt%u template array at %#x (%#x dwords)\n",
8174                     j, faddr, risc_size);
8175                 if (!risc_size || !~risc_size) {
8176                         ql_dbg(ql_dbg_init, vha, 0x0162,
8177                             "-> fwdt%u failed to read array\n", j);
8178                         goto failed;
8179                 }
8180
8181                 /* skip header and ignore checksum */
8182                 faddr += 7;
8183                 risc_size -= 8;
8184
8185                 ql_dbg(ql_dbg_init, vha, 0x0163,
8186                     "-> fwdt%u template allocate template %#x words...\n",
8187                     j, risc_size);
8188                 fwdt->template = vmalloc(risc_size * sizeof(*dcode));
8189                 if (!fwdt->template) {
8190                         ql_log(ql_log_warn, vha, 0x0164,
8191                             "-> fwdt%u failed allocate template.\n", j);
8192                         goto failed;
8193                 }
8194
8195                 dcode = fwdt->template;
8196                 qla24xx_read_flash_data(vha, dcode, faddr, risc_size);
8197
8198                 if (!qla27xx_fwdt_template_valid(dcode)) {
8199                         ql_log(ql_log_warn, vha, 0x0165,
8200                             "-> fwdt%u failed template validate\n", j);
8201                         goto failed;
8202                 }
8203
8204                 dlen = qla27xx_fwdt_template_size(dcode);
8205                 ql_dbg(ql_dbg_init, vha, 0x0166,
8206                     "-> fwdt%u template size %#lx bytes (%#lx words)\n",
8207                     j, dlen, dlen / sizeof(*dcode));
8208                 if (dlen > risc_size * sizeof(*dcode)) {
8209                         ql_log(ql_log_warn, vha, 0x0167,
8210                             "-> fwdt%u template exceeds array (%-lu bytes)\n",
8211                             j, dlen - risc_size * sizeof(*dcode));
8212                         goto failed;
8213                 }
8214
8215                 fwdt->length = dlen;
8216                 ql_dbg(ql_dbg_init, vha, 0x0168,
8217                     "-> fwdt%u loaded template ok\n", j);
8218
8219                 faddr += risc_size + 1;
8220         }
8221
8222         return QLA_SUCCESS;
8223
8224 failed:
8225         vfree(fwdt->template);
8226         fwdt->template = NULL;
8227         fwdt->length = 0;
8228
8229         return QLA_SUCCESS;
8230 }
8231
8232 #define QLA_FW_URL "http://ldriver.qlogic.com/firmware/"
8233
8234 int
8235 qla2x00_load_risc(scsi_qla_host_t *vha, uint32_t *srisc_addr)
8236 {
8237         int     rval;
8238         int     i, fragment;
8239         uint16_t *wcode;
8240         __be16   *fwcode;
8241         uint32_t risc_addr, risc_size, fwclen, wlen, *seg;
8242         struct fw_blob *blob;
8243         struct qla_hw_data *ha = vha->hw;
8244         struct req_que *req = ha->req_q_map[0];
8245
8246         /* Load firmware blob. */
8247         blob = qla2x00_request_firmware(vha);
8248         if (!blob) {
8249                 ql_log(ql_log_info, vha, 0x0083,
8250                     "Firmware image unavailable.\n");
8251                 ql_log(ql_log_info, vha, 0x0084,
8252                     "Firmware images can be retrieved from: "QLA_FW_URL ".\n");
8253                 return QLA_FUNCTION_FAILED;
8254         }
8255
8256         rval = QLA_SUCCESS;
8257
8258         wcode = (uint16_t *)req->ring;
8259         *srisc_addr = 0;
8260         fwcode = (__force __be16 *)blob->fw->data;
8261         fwclen = 0;
8262
8263         /* Validate firmware image by checking version. */
8264         if (blob->fw->size < 8 * sizeof(uint16_t)) {
8265                 ql_log(ql_log_fatal, vha, 0x0085,
8266                     "Unable to verify integrity of firmware image (%zd).\n",
8267                     blob->fw->size);
8268                 goto fail_fw_integrity;
8269         }
8270         for (i = 0; i < 4; i++)
8271                 wcode[i] = be16_to_cpu(fwcode[i + 4]);
8272         if ((wcode[0] == 0xffff && wcode[1] == 0xffff && wcode[2] == 0xffff &&
8273             wcode[3] == 0xffff) || (wcode[0] == 0 && wcode[1] == 0 &&
8274                 wcode[2] == 0 && wcode[3] == 0)) {
8275                 ql_log(ql_log_fatal, vha, 0x0086,
8276                     "Unable to verify integrity of firmware image.\n");
8277                 ql_log(ql_log_fatal, vha, 0x0087,
8278                     "Firmware data: %04x %04x %04x %04x.\n",
8279                     wcode[0], wcode[1], wcode[2], wcode[3]);
8280                 goto fail_fw_integrity;
8281         }
8282
8283         seg = blob->segs;
8284         while (*seg && rval == QLA_SUCCESS) {
8285                 risc_addr = *seg;
8286                 *srisc_addr = *srisc_addr == 0 ? *seg : *srisc_addr;
8287                 risc_size = be16_to_cpu(fwcode[3]);
8288
8289                 /* Validate firmware image size. */
8290                 fwclen += risc_size * sizeof(uint16_t);
8291                 if (blob->fw->size < fwclen) {
8292                         ql_log(ql_log_fatal, vha, 0x0088,
8293                             "Unable to verify integrity of firmware image "
8294                             "(%zd).\n", blob->fw->size);
8295                         goto fail_fw_integrity;
8296                 }
8297
8298                 fragment = 0;
8299                 while (risc_size > 0 && rval == QLA_SUCCESS) {
8300                         wlen = (uint16_t)(ha->fw_transfer_size >> 1);
8301                         if (wlen > risc_size)
8302                                 wlen = risc_size;
8303                         ql_dbg(ql_dbg_init, vha, 0x0089,
8304                             "Loading risc segment@ risc addr %x number of "
8305                             "words 0x%x.\n", risc_addr, wlen);
8306
8307                         for (i = 0; i < wlen; i++)
8308                                 wcode[i] = swab16((__force u32)fwcode[i]);
8309
8310                         rval = qla2x00_load_ram(vha, req->dma, risc_addr,
8311                             wlen);
8312                         if (rval) {
8313                                 ql_log(ql_log_fatal, vha, 0x008a,
8314                                     "Failed to load segment %d of firmware.\n",
8315                                     fragment);
8316                                 break;
8317                         }
8318
8319                         fwcode += wlen;
8320                         risc_addr += wlen;
8321                         risc_size -= wlen;
8322                         fragment++;
8323                 }
8324
8325                 /* Next segment. */
8326                 seg++;
8327         }
8328         return rval;
8329
8330 fail_fw_integrity:
8331         return QLA_FUNCTION_FAILED;
8332 }
8333
8334 static int
8335 qla24xx_load_risc_blob(scsi_qla_host_t *vha, uint32_t *srisc_addr)
8336 {
8337         int     rval;
8338         uint templates, segments, fragment;
8339         uint32_t *dcode;
8340         ulong dlen;
8341         uint32_t risc_addr, risc_size, risc_attr = 0;
8342         ulong i;
8343         uint j;
8344         struct fw_blob *blob;
8345         __be32 *fwcode;
8346         struct qla_hw_data *ha = vha->hw;
8347         struct req_que *req = ha->req_q_map[0];
8348         struct fwdt *fwdt = ha->fwdt;
8349
8350         ql_dbg(ql_dbg_init, vha, 0x0090,
8351             "-> FW: Loading via request-firmware.\n");
8352
8353         blob = qla2x00_request_firmware(vha);
8354         if (!blob) {
8355                 ql_log(ql_log_warn, vha, 0x0092,
8356                     "-> Firmware file not found.\n");
8357
8358                 return QLA_FUNCTION_FAILED;
8359         }
8360
8361         fwcode = (__force __be32 *)blob->fw->data;
8362         dcode = (__force uint32_t *)fwcode;
8363         if (qla24xx_risc_firmware_invalid(dcode)) {
8364                 ql_log(ql_log_fatal, vha, 0x0093,
8365                     "Unable to verify integrity of firmware image (%zd).\n",
8366                     blob->fw->size);
8367                 ql_log(ql_log_fatal, vha, 0x0095,
8368                     "Firmware data: %08x %08x %08x %08x.\n",
8369                     dcode[0], dcode[1], dcode[2], dcode[3]);
8370                 return QLA_FUNCTION_FAILED;
8371         }
8372
8373         dcode = (uint32_t *)req->ring;
8374         *srisc_addr = 0;
8375         segments = FA_RISC_CODE_SEGMENTS;
8376         for (j = 0; j < segments; j++) {
8377                 ql_dbg(ql_dbg_init, vha, 0x0096,
8378                     "-> Loading segment %u...\n", j);
8379                 risc_addr = be32_to_cpu(fwcode[2]);
8380                 risc_size = be32_to_cpu(fwcode[3]);
8381
8382                 if (!*srisc_addr) {
8383                         *srisc_addr = risc_addr;
8384                         risc_attr = be32_to_cpu(fwcode[9]);
8385                 }
8386
8387                 dlen = ha->fw_transfer_size >> 2;
8388                 for (fragment = 0; risc_size; fragment++) {
8389                         if (dlen > risc_size)
8390                                 dlen = risc_size;
8391
8392                         ql_dbg(ql_dbg_init, vha, 0x0097,
8393                             "-> Loading fragment %u: %#x <- %#x (%#lx words)...\n",
8394                             fragment, risc_addr,
8395                             (uint32_t)(fwcode - (typeof(fwcode))blob->fw->data),
8396                             dlen);
8397
8398                         for (i = 0; i < dlen; i++)
8399                                 dcode[i] = swab32((__force u32)fwcode[i]);
8400
8401                         rval = qla2x00_load_ram(vha, req->dma, risc_addr, dlen);
8402                         if (rval) {
8403                                 ql_log(ql_log_fatal, vha, 0x0098,
8404                                     "-> Failed load firmware fragment %u.\n",
8405                                     fragment);
8406                                 return QLA_FUNCTION_FAILED;
8407                         }
8408
8409                         fwcode += dlen;
8410                         risc_addr += dlen;
8411                         risc_size -= dlen;
8412                 }
8413         }
8414
8415         if (!IS_QLA27XX(ha) && !IS_QLA28XX(ha))
8416                 return QLA_SUCCESS;
8417
8418         templates = (risc_attr & BIT_9) ? 2 : 1;
8419         ql_dbg(ql_dbg_init, vha, 0x0170, "-> templates = %u\n", templates);
8420         for (j = 0; j < templates; j++, fwdt++) {
8421                 vfree(fwdt->template);
8422                 fwdt->template = NULL;
8423                 fwdt->length = 0;
8424
8425                 risc_size = be32_to_cpu(fwcode[2]);
8426                 ql_dbg(ql_dbg_init, vha, 0x0171,
8427                     "-> fwdt%u template array at %#x (%#x dwords)\n",
8428                     j, (uint32_t)((void *)fwcode - (void *)blob->fw->data),
8429                     risc_size);
8430                 if (!risc_size || !~risc_size) {
8431                         ql_dbg(ql_dbg_init, vha, 0x0172,
8432                             "-> fwdt%u failed to read array\n", j);
8433                         goto failed;
8434                 }
8435
8436                 /* skip header and ignore checksum */
8437                 fwcode += 7;
8438                 risc_size -= 8;
8439
8440                 ql_dbg(ql_dbg_init, vha, 0x0173,
8441                     "-> fwdt%u template allocate template %#x words...\n",
8442                     j, risc_size);
8443                 fwdt->template = vmalloc(risc_size * sizeof(*dcode));
8444                 if (!fwdt->template) {
8445                         ql_log(ql_log_warn, vha, 0x0174,
8446                             "-> fwdt%u failed allocate template.\n", j);
8447                         goto failed;
8448                 }
8449
8450                 dcode = fwdt->template;
8451                 for (i = 0; i < risc_size; i++)
8452                         dcode[i] = (__force u32)fwcode[i];
8453
8454                 if (!qla27xx_fwdt_template_valid(dcode)) {
8455                         ql_log(ql_log_warn, vha, 0x0175,
8456                             "-> fwdt%u failed template validate\n", j);
8457                         goto failed;
8458                 }
8459
8460                 dlen = qla27xx_fwdt_template_size(dcode);
8461                 ql_dbg(ql_dbg_init, vha, 0x0176,
8462                     "-> fwdt%u template size %#lx bytes (%#lx words)\n",
8463                     j, dlen, dlen / sizeof(*dcode));
8464                 if (dlen > risc_size * sizeof(*dcode)) {
8465                         ql_log(ql_log_warn, vha, 0x0177,
8466                             "-> fwdt%u template exceeds array (%-lu bytes)\n",
8467                             j, dlen - risc_size * sizeof(*dcode));
8468                         goto failed;
8469                 }
8470
8471                 fwdt->length = dlen;
8472                 ql_dbg(ql_dbg_init, vha, 0x0178,
8473                     "-> fwdt%u loaded template ok\n", j);
8474
8475                 fwcode += risc_size + 1;
8476         }
8477
8478         return QLA_SUCCESS;
8479
8480 failed:
8481         vfree(fwdt->template);
8482         fwdt->template = NULL;
8483         fwdt->length = 0;
8484
8485         return QLA_SUCCESS;
8486 }
8487
8488 int
8489 qla24xx_load_risc(scsi_qla_host_t *vha, uint32_t *srisc_addr)
8490 {
8491         int rval;
8492
8493         if (ql2xfwloadbin == 1)
8494                 return qla81xx_load_risc(vha, srisc_addr);
8495
8496         /*
8497          * FW Load priority:
8498          * 1) Firmware via request-firmware interface (.bin file).
8499          * 2) Firmware residing in flash.
8500          */
8501         rval = qla24xx_load_risc_blob(vha, srisc_addr);
8502         if (rval == QLA_SUCCESS)
8503                 return rval;
8504
8505         return qla24xx_load_risc_flash(vha, srisc_addr,
8506             vha->hw->flt_region_fw);
8507 }
8508
8509 int
8510 qla81xx_load_risc(scsi_qla_host_t *vha, uint32_t *srisc_addr)
8511 {
8512         int rval;
8513         struct qla_hw_data *ha = vha->hw;
8514         struct active_regions active_regions = { };
8515
8516         if (ql2xfwloadbin == 2)
8517                 goto try_blob_fw;
8518
8519         /* FW Load priority:
8520          * 1) Firmware residing in flash.
8521          * 2) Firmware via request-firmware interface (.bin file).
8522          * 3) Golden-Firmware residing in flash -- (limited operation).
8523          */
8524
8525         if (!IS_QLA27XX(ha) && !IS_QLA28XX(ha))
8526                 goto try_primary_fw;
8527
8528         qla27xx_get_active_image(vha, &active_regions);
8529
8530         if (active_regions.global != QLA27XX_SECONDARY_IMAGE)
8531                 goto try_primary_fw;
8532
8533         ql_dbg(ql_dbg_init, vha, 0x008b,
8534             "Loading secondary firmware image.\n");
8535         rval = qla24xx_load_risc_flash(vha, srisc_addr, ha->flt_region_fw_sec);
8536         if (!rval)
8537                 return rval;
8538
8539 try_primary_fw:
8540         ql_dbg(ql_dbg_init, vha, 0x008b,
8541             "Loading primary firmware image.\n");
8542         rval = qla24xx_load_risc_flash(vha, srisc_addr, ha->flt_region_fw);
8543         if (!rval)
8544                 return rval;
8545
8546 try_blob_fw:
8547         rval = qla24xx_load_risc_blob(vha, srisc_addr);
8548         if (!rval || !ha->flt_region_gold_fw)
8549                 return rval;
8550
8551         ql_log(ql_log_info, vha, 0x0099,
8552             "Attempting to fallback to golden firmware.\n");
8553         rval = qla24xx_load_risc_flash(vha, srisc_addr, ha->flt_region_gold_fw);
8554         if (rval)
8555                 return rval;
8556
8557         ql_log(ql_log_info, vha, 0x009a, "Need firmware flash update.\n");
8558         ha->flags.running_gold_fw = 1;
8559         return rval;
8560 }
8561
8562 void
8563 qla2x00_try_to_stop_firmware(scsi_qla_host_t *vha)
8564 {
8565         int ret, retries;
8566         struct qla_hw_data *ha = vha->hw;
8567
8568         if (ha->flags.pci_channel_io_perm_failure)
8569                 return;
8570         if (!IS_FWI2_CAPABLE(ha))
8571                 return;
8572         if (!ha->fw_major_version)
8573                 return;
8574         if (!ha->flags.fw_started)
8575                 return;
8576
8577         ret = qla2x00_stop_firmware(vha);
8578         for (retries = 5; ret != QLA_SUCCESS && ret != QLA_FUNCTION_TIMEOUT &&
8579             ret != QLA_INVALID_COMMAND && retries ; retries--) {
8580                 ha->isp_ops->reset_chip(vha);
8581                 if (ha->isp_ops->chip_diag(vha) != QLA_SUCCESS)
8582                         continue;
8583                 if (qla2x00_setup_chip(vha) != QLA_SUCCESS)
8584                         continue;
8585                 ql_log(ql_log_info, vha, 0x8015,
8586                     "Attempting retry of stop-firmware command.\n");
8587                 ret = qla2x00_stop_firmware(vha);
8588         }
8589
8590         QLA_FW_STOPPED(ha);
8591         ha->flags.fw_init_done = 0;
8592 }
8593
8594 int
8595 qla24xx_configure_vhba(scsi_qla_host_t *vha)
8596 {
8597         int rval = QLA_SUCCESS;
8598         int rval2;
8599         uint16_t mb[MAILBOX_REGISTER_COUNT];
8600         struct qla_hw_data *ha = vha->hw;
8601         struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
8602
8603         if (!vha->vp_idx)
8604                 return -EINVAL;
8605
8606         rval = qla2x00_fw_ready(base_vha);
8607
8608         if (rval == QLA_SUCCESS) {
8609                 clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
8610                 qla2x00_marker(vha, ha->base_qpair, 0, 0, MK_SYNC_ALL);
8611         }
8612
8613         vha->flags.management_server_logged_in = 0;
8614
8615         /* Login to SNS first */
8616         rval2 = ha->isp_ops->fabric_login(vha, NPH_SNS, 0xff, 0xff, 0xfc, mb,
8617             BIT_1);
8618         if (rval2 != QLA_SUCCESS || mb[0] != MBS_COMMAND_COMPLETE) {
8619                 if (rval2 == QLA_MEMORY_ALLOC_FAILED)
8620                         ql_dbg(ql_dbg_init, vha, 0x0120,
8621                             "Failed SNS login: loop_id=%x, rval2=%d\n",
8622                             NPH_SNS, rval2);
8623                 else
8624                         ql_dbg(ql_dbg_init, vha, 0x0103,
8625                             "Failed SNS login: loop_id=%x mb[0]=%x mb[1]=%x "
8626                             "mb[2]=%x mb[6]=%x mb[7]=%x.\n",
8627                             NPH_SNS, mb[0], mb[1], mb[2], mb[6], mb[7]);
8628                 return (QLA_FUNCTION_FAILED);
8629         }
8630
8631         atomic_set(&vha->loop_down_timer, 0);
8632         atomic_set(&vha->loop_state, LOOP_UP);
8633         set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
8634         set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
8635         rval = qla2x00_loop_resync(base_vha);
8636
8637         return rval;
8638 }
8639
8640 /* 84XX Support **************************************************************/
8641
8642 static LIST_HEAD(qla_cs84xx_list);
8643 static DEFINE_MUTEX(qla_cs84xx_mutex);
8644
8645 static struct qla_chip_state_84xx *
8646 qla84xx_get_chip(struct scsi_qla_host *vha)
8647 {
8648         struct qla_chip_state_84xx *cs84xx;
8649         struct qla_hw_data *ha = vha->hw;
8650
8651         mutex_lock(&qla_cs84xx_mutex);
8652
8653         /* Find any shared 84xx chip. */
8654         list_for_each_entry(cs84xx, &qla_cs84xx_list, list) {
8655                 if (cs84xx->bus == ha->pdev->bus) {
8656                         kref_get(&cs84xx->kref);
8657                         goto done;
8658                 }
8659         }
8660
8661         cs84xx = kzalloc(sizeof(*cs84xx), GFP_KERNEL);
8662         if (!cs84xx)
8663                 goto done;
8664
8665         kref_init(&cs84xx->kref);
8666         spin_lock_init(&cs84xx->access_lock);
8667         mutex_init(&cs84xx->fw_update_mutex);
8668         cs84xx->bus = ha->pdev->bus;
8669
8670         list_add_tail(&cs84xx->list, &qla_cs84xx_list);
8671 done:
8672         mutex_unlock(&qla_cs84xx_mutex);
8673         return cs84xx;
8674 }
8675
8676 static void
8677 __qla84xx_chip_release(struct kref *kref)
8678 {
8679         struct qla_chip_state_84xx *cs84xx =
8680             container_of(kref, struct qla_chip_state_84xx, kref);
8681
8682         mutex_lock(&qla_cs84xx_mutex);
8683         list_del(&cs84xx->list);
8684         mutex_unlock(&qla_cs84xx_mutex);
8685         kfree(cs84xx);
8686 }
8687
8688 void
8689 qla84xx_put_chip(struct scsi_qla_host *vha)
8690 {
8691         struct qla_hw_data *ha = vha->hw;
8692
8693         if (ha->cs84xx)
8694                 kref_put(&ha->cs84xx->kref, __qla84xx_chip_release);
8695 }
8696
8697 static int
8698 qla84xx_init_chip(scsi_qla_host_t *vha)
8699 {
8700         int rval;
8701         uint16_t status[2];
8702         struct qla_hw_data *ha = vha->hw;
8703
8704         mutex_lock(&ha->cs84xx->fw_update_mutex);
8705
8706         rval = qla84xx_verify_chip(vha, status);
8707
8708         mutex_unlock(&ha->cs84xx->fw_update_mutex);
8709
8710         return rval != QLA_SUCCESS || status[0] ? QLA_FUNCTION_FAILED :
8711             QLA_SUCCESS;
8712 }
8713
8714 /* 81XX Support **************************************************************/
8715
8716 int
8717 qla81xx_nvram_config(scsi_qla_host_t *vha)
8718 {
8719         int   rval;
8720         struct init_cb_81xx *icb;
8721         struct nvram_81xx *nv;
8722         __le32 *dptr;
8723         uint8_t  *dptr1, *dptr2;
8724         uint32_t chksum;
8725         uint16_t cnt;
8726         struct qla_hw_data *ha = vha->hw;
8727         uint32_t faddr;
8728         struct active_regions active_regions = { };
8729
8730         rval = QLA_SUCCESS;
8731         icb = (struct init_cb_81xx *)ha->init_cb;
8732         nv = ha->nvram;
8733
8734         /* Determine NVRAM starting address. */
8735         ha->nvram_size = sizeof(*nv);
8736         ha->vpd_size = FA_NVRAM_VPD_SIZE;
8737         if (IS_P3P_TYPE(ha) || IS_QLA8031(ha))
8738                 ha->vpd_size = FA_VPD_SIZE_82XX;
8739
8740         if (IS_QLA28XX(ha) || IS_QLA27XX(ha))
8741                 qla28xx_get_aux_images(vha, &active_regions);
8742
8743         /* Get VPD data into cache */
8744         ha->vpd = ha->nvram + VPD_OFFSET;
8745
8746         faddr = ha->flt_region_vpd;
8747         if (IS_QLA28XX(ha)) {
8748                 if (active_regions.aux.vpd_nvram == QLA27XX_SECONDARY_IMAGE)
8749                         faddr = ha->flt_region_vpd_sec;
8750                 ql_dbg(ql_dbg_init, vha, 0x0110,
8751                     "Loading %s nvram image.\n",
8752                     active_regions.aux.vpd_nvram == QLA27XX_PRIMARY_IMAGE ?
8753                     "primary" : "secondary");
8754         }
8755         ha->isp_ops->read_optrom(vha, ha->vpd, faddr << 2, ha->vpd_size);
8756
8757         /* Get NVRAM data into cache and calculate checksum. */
8758         faddr = ha->flt_region_nvram;
8759         if (IS_QLA28XX(ha)) {
8760                 if (active_regions.aux.vpd_nvram == QLA27XX_SECONDARY_IMAGE)
8761                         faddr = ha->flt_region_nvram_sec;
8762         }
8763         ql_dbg(ql_dbg_init, vha, 0x0110,
8764             "Loading %s nvram image.\n",
8765             active_regions.aux.vpd_nvram == QLA27XX_PRIMARY_IMAGE ?
8766             "primary" : "secondary");
8767         ha->isp_ops->read_optrom(vha, ha->nvram, faddr << 2, ha->nvram_size);
8768
8769         dptr = (__force __le32 *)nv;
8770         for (cnt = 0, chksum = 0; cnt < ha->nvram_size >> 2; cnt++, dptr++)
8771                 chksum += le32_to_cpu(*dptr);
8772
8773         ql_dbg(ql_dbg_init + ql_dbg_buffer, vha, 0x0111,
8774             "Contents of NVRAM:\n");
8775         ql_dump_buffer(ql_dbg_init + ql_dbg_buffer, vha, 0x0112,
8776             nv, ha->nvram_size);
8777
8778         /* Bad NVRAM data, set defaults parameters. */
8779         if (chksum || memcmp("ISP ", nv->id, sizeof(nv->id)) ||
8780             le16_to_cpu(nv->nvram_version) < ICB_VERSION) {
8781                 /* Reset NVRAM data. */
8782                 ql_log(ql_log_info, vha, 0x0073,
8783                     "Inconsistent NVRAM checksum=%#x id=%.4s version=%#x.\n",
8784                     chksum, nv->id, le16_to_cpu(nv->nvram_version));
8785                 ql_dump_buffer(ql_dbg_init, vha, 0x0073, nv, sizeof(*nv));
8786                 ql_log(ql_log_info, vha, 0x0074,
8787                     "Falling back to functioning (yet invalid -- WWPN) "
8788                     "defaults.\n");
8789
8790                 /*
8791                  * Set default initialization control block.
8792                  */
8793                 memset(nv, 0, ha->nvram_size);
8794                 nv->nvram_version = cpu_to_le16(ICB_VERSION);
8795                 nv->version = cpu_to_le16(ICB_VERSION);
8796                 nv->frame_payload_size = cpu_to_le16(2048);
8797                 nv->execution_throttle = cpu_to_le16(0xFFFF);
8798                 nv->exchange_count = cpu_to_le16(0);
8799                 nv->port_name[0] = 0x21;
8800                 nv->port_name[1] = 0x00 + ha->port_no + 1;
8801                 nv->port_name[2] = 0x00;
8802                 nv->port_name[3] = 0xe0;
8803                 nv->port_name[4] = 0x8b;
8804                 nv->port_name[5] = 0x1c;
8805                 nv->port_name[6] = 0x55;
8806                 nv->port_name[7] = 0x86;
8807                 nv->node_name[0] = 0x20;
8808                 nv->node_name[1] = 0x00;
8809                 nv->node_name[2] = 0x00;
8810                 nv->node_name[3] = 0xe0;
8811                 nv->node_name[4] = 0x8b;
8812                 nv->node_name[5] = 0x1c;
8813                 nv->node_name[6] = 0x55;
8814                 nv->node_name[7] = 0x86;
8815                 nv->login_retry_count = cpu_to_le16(8);
8816                 nv->interrupt_delay_timer = cpu_to_le16(0);
8817                 nv->login_timeout = cpu_to_le16(0);
8818                 nv->firmware_options_1 =
8819                     cpu_to_le32(BIT_14|BIT_13|BIT_2|BIT_1);
8820                 nv->firmware_options_2 = cpu_to_le32(2 << 4);
8821                 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
8822                 nv->firmware_options_3 = cpu_to_le32(2 << 13);
8823                 nv->host_p = cpu_to_le32(BIT_11|BIT_10);
8824                 nv->efi_parameters = cpu_to_le32(0);
8825                 nv->reset_delay = 5;
8826                 nv->max_luns_per_target = cpu_to_le16(128);
8827                 nv->port_down_retry_count = cpu_to_le16(30);
8828                 nv->link_down_timeout = cpu_to_le16(180);
8829                 nv->enode_mac[0] = 0x00;
8830                 nv->enode_mac[1] = 0xC0;
8831                 nv->enode_mac[2] = 0xDD;
8832                 nv->enode_mac[3] = 0x04;
8833                 nv->enode_mac[4] = 0x05;
8834                 nv->enode_mac[5] = 0x06 + ha->port_no + 1;
8835
8836                 rval = 1;
8837         }
8838
8839         if (IS_T10_PI_CAPABLE(ha))
8840                 nv->frame_payload_size &= cpu_to_le16(~7);
8841
8842         qlt_81xx_config_nvram_stage1(vha, nv);
8843
8844         /* Reset Initialization control block */
8845         memset(icb, 0, ha->init_cb_size);
8846
8847         /* Copy 1st segment. */
8848         dptr1 = (uint8_t *)icb;
8849         dptr2 = (uint8_t *)&nv->version;
8850         cnt = (uint8_t *)&icb->response_q_inpointer - (uint8_t *)&icb->version;
8851         while (cnt--)
8852                 *dptr1++ = *dptr2++;
8853
8854         icb->login_retry_count = nv->login_retry_count;
8855
8856         /* Copy 2nd segment. */
8857         dptr1 = (uint8_t *)&icb->interrupt_delay_timer;
8858         dptr2 = (uint8_t *)&nv->interrupt_delay_timer;
8859         cnt = (uint8_t *)&icb->reserved_5 -
8860             (uint8_t *)&icb->interrupt_delay_timer;
8861         while (cnt--)
8862                 *dptr1++ = *dptr2++;
8863
8864         memcpy(icb->enode_mac, nv->enode_mac, sizeof(icb->enode_mac));
8865         /* Some boards (with valid NVRAMs) still have NULL enode_mac!! */
8866         if (!memcmp(icb->enode_mac, "\0\0\0\0\0\0", sizeof(icb->enode_mac))) {
8867                 icb->enode_mac[0] = 0x00;
8868                 icb->enode_mac[1] = 0xC0;
8869                 icb->enode_mac[2] = 0xDD;
8870                 icb->enode_mac[3] = 0x04;
8871                 icb->enode_mac[4] = 0x05;
8872                 icb->enode_mac[5] = 0x06 + ha->port_no + 1;
8873         }
8874
8875         /* Use extended-initialization control block. */
8876         memcpy(ha->ex_init_cb, &nv->ex_version, sizeof(*ha->ex_init_cb));
8877         ha->frame_payload_size = le16_to_cpu(icb->frame_payload_size);
8878         /*
8879          * Setup driver NVRAM options.
8880          */
8881         qla2x00_set_model_info(vha, nv->model_name, sizeof(nv->model_name),
8882             "QLE8XXX");
8883
8884         qlt_81xx_config_nvram_stage2(vha, icb);
8885
8886         /* Use alternate WWN? */
8887         if (nv->host_p & cpu_to_le32(BIT_15)) {
8888                 memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
8889                 memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
8890         }
8891
8892         /* Prepare nodename */
8893         if ((icb->firmware_options_1 & cpu_to_le32(BIT_14)) == 0) {
8894                 /*
8895                  * Firmware will apply the following mask if the nodename was
8896                  * not provided.
8897                  */
8898                 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
8899                 icb->node_name[0] &= 0xF0;
8900         }
8901
8902         if (IS_QLA28XX(ha) || IS_QLA27XX(ha)) {
8903                 if ((nv->enhanced_features & BIT_7) == 0)
8904                         ha->flags.scm_supported_a = 1;
8905         }
8906
8907         /* Set host adapter parameters. */
8908         ha->flags.disable_risc_code_load = 0;
8909         ha->flags.enable_lip_reset = 0;
8910         ha->flags.enable_lip_full_login =
8911             le32_to_cpu(nv->host_p) & BIT_10 ? 1 : 0;
8912         ha->flags.enable_target_reset =
8913             le32_to_cpu(nv->host_p) & BIT_11 ? 1 : 0;
8914         ha->flags.enable_led_scheme = 0;
8915         ha->flags.disable_serdes = le32_to_cpu(nv->host_p) & BIT_5 ? 1 : 0;
8916
8917         ha->operating_mode = (le32_to_cpu(icb->firmware_options_2) &
8918             (BIT_6 | BIT_5 | BIT_4)) >> 4;
8919
8920         /* save HBA serial number */
8921         ha->serial0 = icb->port_name[5];
8922         ha->serial1 = icb->port_name[6];
8923         ha->serial2 = icb->port_name[7];
8924         memcpy(vha->node_name, icb->node_name, WWN_SIZE);
8925         memcpy(vha->port_name, icb->port_name, WWN_SIZE);
8926
8927         icb->execution_throttle = cpu_to_le16(0xFFFF);
8928
8929         ha->retry_count = le16_to_cpu(nv->login_retry_count);
8930
8931         /* Set minimum login_timeout to 4 seconds. */
8932         if (le16_to_cpu(nv->login_timeout) < ql2xlogintimeout)
8933                 nv->login_timeout = cpu_to_le16(ql2xlogintimeout);
8934         if (le16_to_cpu(nv->login_timeout) < 4)
8935                 nv->login_timeout = cpu_to_le16(4);
8936         ha->login_timeout = le16_to_cpu(nv->login_timeout);
8937
8938         /* Set minimum RATOV to 100 tenths of a second. */
8939         ha->r_a_tov = 100;
8940
8941         ha->loop_reset_delay = nv->reset_delay;
8942
8943         /* Link Down Timeout = 0:
8944          *
8945          *      When Port Down timer expires we will start returning
8946          *      I/O's to OS with "DID_NO_CONNECT".
8947          *
8948          * Link Down Timeout != 0:
8949          *
8950          *       The driver waits for the link to come up after link down
8951          *       before returning I/Os to OS with "DID_NO_CONNECT".
8952          */
8953         if (le16_to_cpu(nv->link_down_timeout) == 0) {
8954                 ha->loop_down_abort_time =
8955                     (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
8956         } else {
8957                 ha->link_down_timeout = le16_to_cpu(nv->link_down_timeout);
8958                 ha->loop_down_abort_time =
8959                     (LOOP_DOWN_TIME - ha->link_down_timeout);
8960         }
8961
8962         /* Need enough time to try and get the port back. */
8963         ha->port_down_retry_count = le16_to_cpu(nv->port_down_retry_count);
8964         if (qlport_down_retry)
8965                 ha->port_down_retry_count = qlport_down_retry;
8966
8967         /* Set login_retry_count */
8968         ha->login_retry_count  = le16_to_cpu(nv->login_retry_count);
8969         if (ha->port_down_retry_count ==
8970             le16_to_cpu(nv->port_down_retry_count) &&
8971             ha->port_down_retry_count > 3)
8972                 ha->login_retry_count = ha->port_down_retry_count;
8973         else if (ha->port_down_retry_count > (int)ha->login_retry_count)
8974                 ha->login_retry_count = ha->port_down_retry_count;
8975         if (ql2xloginretrycount)
8976                 ha->login_retry_count = ql2xloginretrycount;
8977
8978         /* if not running MSI-X we need handshaking on interrupts */
8979         if (!vha->hw->flags.msix_enabled &&
8980             (IS_QLA83XX(ha) || IS_QLA27XX(ha) || IS_QLA28XX(ha)))
8981                 icb->firmware_options_2 |= cpu_to_le32(BIT_22);
8982
8983         /* Enable ZIO. */
8984         if (!vha->flags.init_done) {
8985                 ha->zio_mode = le32_to_cpu(icb->firmware_options_2) &
8986                     (BIT_3 | BIT_2 | BIT_1 | BIT_0);
8987                 ha->zio_timer = le16_to_cpu(icb->interrupt_delay_timer) ?
8988                     le16_to_cpu(icb->interrupt_delay_timer) : 2;
8989         }
8990         icb->firmware_options_2 &= cpu_to_le32(
8991             ~(BIT_3 | BIT_2 | BIT_1 | BIT_0));
8992         vha->flags.process_response_queue = 0;
8993         if (ha->zio_mode != QLA_ZIO_DISABLED) {
8994                 ha->zio_mode = QLA_ZIO_MODE_6;
8995
8996                 ql_log(ql_log_info, vha, 0x0075,
8997                     "ZIO mode %d enabled; timer delay (%d us).\n",
8998                     ha->zio_mode,
8999                     ha->zio_timer * 100);
9000
9001                 icb->firmware_options_2 |= cpu_to_le32(
9002                     (uint32_t)ha->zio_mode);
9003                 icb->interrupt_delay_timer = cpu_to_le16(ha->zio_timer);
9004                 vha->flags.process_response_queue = 1;
9005         }
9006
9007          /* enable RIDA Format2 */
9008         icb->firmware_options_3 |= cpu_to_le32(BIT_0);
9009
9010         /* N2N: driver will initiate Login instead of FW */
9011         icb->firmware_options_3 |= cpu_to_le32(BIT_8);
9012
9013         /* Determine NVMe/FCP priority for target ports */
9014         ha->fc4_type_priority = qla2xxx_get_fc4_priority(vha);
9015
9016         if (rval) {
9017                 ql_log(ql_log_warn, vha, 0x0076,
9018                     "NVRAM configuration failed.\n");
9019         }
9020         return (rval);
9021 }
9022
9023 int
9024 qla82xx_restart_isp(scsi_qla_host_t *vha)
9025 {
9026         int status, rval;
9027         struct qla_hw_data *ha = vha->hw;
9028         struct scsi_qla_host *vp, *tvp;
9029         unsigned long flags;
9030
9031         status = qla2x00_init_rings(vha);
9032         if (!status) {
9033                 clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
9034                 ha->flags.chip_reset_done = 1;
9035
9036                 status = qla2x00_fw_ready(vha);
9037                 if (!status) {
9038                         /* Issue a marker after FW becomes ready. */
9039                         qla2x00_marker(vha, ha->base_qpair, 0, 0, MK_SYNC_ALL);
9040                         vha->flags.online = 1;
9041                         set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
9042                 }
9043
9044                 /* if no cable then assume it's good */
9045                 if ((vha->device_flags & DFLG_NO_CABLE))
9046                         status = 0;
9047         }
9048
9049         if (!status) {
9050                 clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
9051
9052                 if (!atomic_read(&vha->loop_down_timer)) {
9053                         /*
9054                          * Issue marker command only when we are going
9055                          * to start the I/O .
9056                          */
9057                         vha->marker_needed = 1;
9058                 }
9059
9060                 ha->isp_ops->enable_intrs(ha);
9061
9062                 ha->isp_abort_cnt = 0;
9063                 clear_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
9064
9065                 /* Update the firmware version */
9066                 status = qla82xx_check_md_needed(vha);
9067
9068                 if (ha->fce) {
9069                         ha->flags.fce_enabled = 1;
9070                         memset(ha->fce, 0,
9071                             fce_calc_size(ha->fce_bufs));
9072                         rval = qla2x00_enable_fce_trace(vha,
9073                             ha->fce_dma, ha->fce_bufs, ha->fce_mb,
9074                             &ha->fce_bufs);
9075                         if (rval) {
9076                                 ql_log(ql_log_warn, vha, 0x8001,
9077                                     "Unable to reinitialize FCE (%d).\n",
9078                                     rval);
9079                                 ha->flags.fce_enabled = 0;
9080                         }
9081                 }
9082
9083                 if (ha->eft) {
9084                         memset(ha->eft, 0, EFT_SIZE);
9085                         rval = qla2x00_enable_eft_trace(vha,
9086                             ha->eft_dma, EFT_NUM_BUFFERS);
9087                         if (rval) {
9088                                 ql_log(ql_log_warn, vha, 0x8010,
9089                                     "Unable to reinitialize EFT (%d).\n",
9090                                     rval);
9091                         }
9092                 }
9093         }
9094
9095         if (!status) {
9096                 ql_dbg(ql_dbg_taskm, vha, 0x8011,
9097                     "qla82xx_restart_isp succeeded.\n");
9098
9099                 spin_lock_irqsave(&ha->vport_slock, flags);
9100                 list_for_each_entry_safe(vp, tvp, &ha->vp_list, list) {
9101                         if (vp->vp_idx) {
9102                                 atomic_inc(&vp->vref_count);
9103                                 spin_unlock_irqrestore(&ha->vport_slock, flags);
9104
9105                                 qla2x00_vp_abort_isp(vp);
9106
9107                                 spin_lock_irqsave(&ha->vport_slock, flags);
9108                                 atomic_dec(&vp->vref_count);
9109                         }
9110                 }
9111                 spin_unlock_irqrestore(&ha->vport_slock, flags);
9112
9113         } else {
9114                 ql_log(ql_log_warn, vha, 0x8016,
9115                     "qla82xx_restart_isp **** FAILED ****.\n");
9116         }
9117
9118         return status;
9119 }
9120
9121 /*
9122  * qla24xx_get_fcp_prio
9123  *      Gets the fcp cmd priority value for the logged in port.
9124  *      Looks for a match of the port descriptors within
9125  *      each of the fcp prio config entries. If a match is found,
9126  *      the tag (priority) value is returned.
9127  *
9128  * Input:
9129  *      vha = scsi host structure pointer.
9130  *      fcport = port structure pointer.
9131  *
9132  * Return:
9133  *      non-zero (if found)
9134  *      -1 (if not found)
9135  *
9136  * Context:
9137  *      Kernel context
9138  */
9139 static int
9140 qla24xx_get_fcp_prio(scsi_qla_host_t *vha, fc_port_t *fcport)
9141 {
9142         int i, entries;
9143         uint8_t pid_match, wwn_match;
9144         int priority;
9145         uint32_t pid1, pid2;
9146         uint64_t wwn1, wwn2;
9147         struct qla_fcp_prio_entry *pri_entry;
9148         struct qla_hw_data *ha = vha->hw;
9149
9150         if (!ha->fcp_prio_cfg || !ha->flags.fcp_prio_enabled)
9151                 return -1;
9152
9153         priority = -1;
9154         entries = ha->fcp_prio_cfg->num_entries;
9155         pri_entry = &ha->fcp_prio_cfg->entry[0];
9156
9157         for (i = 0; i < entries; i++) {
9158                 pid_match = wwn_match = 0;
9159
9160                 if (!(pri_entry->flags & FCP_PRIO_ENTRY_VALID)) {
9161                         pri_entry++;
9162                         continue;
9163                 }
9164
9165                 /* check source pid for a match */
9166                 if (pri_entry->flags & FCP_PRIO_ENTRY_SPID_VALID) {
9167                         pid1 = pri_entry->src_pid & INVALID_PORT_ID;
9168                         pid2 = vha->d_id.b24 & INVALID_PORT_ID;
9169                         if (pid1 == INVALID_PORT_ID)
9170                                 pid_match++;
9171                         else if (pid1 == pid2)
9172                                 pid_match++;
9173                 }
9174
9175                 /* check destination pid for a match */
9176                 if (pri_entry->flags & FCP_PRIO_ENTRY_DPID_VALID) {
9177                         pid1 = pri_entry->dst_pid & INVALID_PORT_ID;
9178                         pid2 = fcport->d_id.b24 & INVALID_PORT_ID;
9179                         if (pid1 == INVALID_PORT_ID)
9180                                 pid_match++;
9181                         else if (pid1 == pid2)
9182                                 pid_match++;
9183                 }
9184
9185                 /* check source WWN for a match */
9186                 if (pri_entry->flags & FCP_PRIO_ENTRY_SWWN_VALID) {
9187                         wwn1 = wwn_to_u64(vha->port_name);
9188                         wwn2 = wwn_to_u64(pri_entry->src_wwpn);
9189                         if (wwn2 == (uint64_t)-1)
9190                                 wwn_match++;
9191                         else if (wwn1 == wwn2)
9192                                 wwn_match++;
9193                 }
9194
9195                 /* check destination WWN for a match */
9196                 if (pri_entry->flags & FCP_PRIO_ENTRY_DWWN_VALID) {
9197                         wwn1 = wwn_to_u64(fcport->port_name);
9198                         wwn2 = wwn_to_u64(pri_entry->dst_wwpn);
9199                         if (wwn2 == (uint64_t)-1)
9200                                 wwn_match++;
9201                         else if (wwn1 == wwn2)
9202                                 wwn_match++;
9203                 }
9204
9205                 if (pid_match == 2 || wwn_match == 2) {
9206                         /* Found a matching entry */
9207                         if (pri_entry->flags & FCP_PRIO_ENTRY_TAG_VALID)
9208                                 priority = pri_entry->tag;
9209                         break;
9210                 }
9211
9212                 pri_entry++;
9213         }
9214
9215         return priority;
9216 }
9217
9218 /*
9219  * qla24xx_update_fcport_fcp_prio
9220  *      Activates fcp priority for the logged in fc port
9221  *
9222  * Input:
9223  *      vha = scsi host structure pointer.
9224  *      fcp = port structure pointer.
9225  *
9226  * Return:
9227  *      QLA_SUCCESS or QLA_FUNCTION_FAILED
9228  *
9229  * Context:
9230  *      Kernel context.
9231  */
9232 int
9233 qla24xx_update_fcport_fcp_prio(scsi_qla_host_t *vha, fc_port_t *fcport)
9234 {
9235         int ret;
9236         int priority;
9237         uint16_t mb[5];
9238
9239         if (fcport->port_type != FCT_TARGET ||
9240             fcport->loop_id == FC_NO_LOOP_ID)
9241                 return QLA_FUNCTION_FAILED;
9242
9243         priority = qla24xx_get_fcp_prio(vha, fcport);
9244         if (priority < 0)
9245                 return QLA_FUNCTION_FAILED;
9246
9247         if (IS_P3P_TYPE(vha->hw)) {
9248                 fcport->fcp_prio = priority & 0xf;
9249                 return QLA_SUCCESS;
9250         }
9251
9252         ret = qla24xx_set_fcp_prio(vha, fcport->loop_id, priority, mb);
9253         if (ret == QLA_SUCCESS) {
9254                 if (fcport->fcp_prio != priority)
9255                         ql_dbg(ql_dbg_user, vha, 0x709e,
9256                             "Updated FCP_CMND priority - value=%d loop_id=%d "
9257                             "port_id=%02x%02x%02x.\n", priority,
9258                             fcport->loop_id, fcport->d_id.b.domain,
9259                             fcport->d_id.b.area, fcport->d_id.b.al_pa);
9260                 fcport->fcp_prio = priority & 0xf;
9261         } else
9262                 ql_dbg(ql_dbg_user, vha, 0x704f,
9263                     "Unable to update FCP_CMND priority - ret=0x%x for "
9264                     "loop_id=%d port_id=%02x%02x%02x.\n", ret, fcport->loop_id,
9265                     fcport->d_id.b.domain, fcport->d_id.b.area,
9266                     fcport->d_id.b.al_pa);
9267         return  ret;
9268 }
9269
9270 /*
9271  * qla24xx_update_all_fcp_prio
9272  *      Activates fcp priority for all the logged in ports
9273  *
9274  * Input:
9275  *      ha = adapter block pointer.
9276  *
9277  * Return:
9278  *      QLA_SUCCESS or QLA_FUNCTION_FAILED
9279  *
9280  * Context:
9281  *      Kernel context.
9282  */
9283 int
9284 qla24xx_update_all_fcp_prio(scsi_qla_host_t *vha)
9285 {
9286         int ret;
9287         fc_port_t *fcport;
9288
9289         ret = QLA_FUNCTION_FAILED;
9290         /* We need to set priority for all logged in ports */
9291         list_for_each_entry(fcport, &vha->vp_fcports, list)
9292                 ret = qla24xx_update_fcport_fcp_prio(vha, fcport);
9293
9294         return ret;
9295 }
9296
9297 struct qla_qpair *qla2xxx_create_qpair(struct scsi_qla_host *vha, int qos,
9298         int vp_idx, bool startqp)
9299 {
9300         int rsp_id = 0;
9301         int  req_id = 0;
9302         int i;
9303         struct qla_hw_data *ha = vha->hw;
9304         uint16_t qpair_id = 0;
9305         struct qla_qpair *qpair = NULL;
9306         struct qla_msix_entry *msix;
9307
9308         if (!(ha->fw_attributes & BIT_6) || !ha->flags.msix_enabled) {
9309                 ql_log(ql_log_warn, vha, 0x00181,
9310                     "FW/Driver is not multi-queue capable.\n");
9311                 return NULL;
9312         }
9313
9314         if (ql2xmqsupport || ql2xnvmeenable) {
9315                 qpair = kzalloc(sizeof(struct qla_qpair), GFP_KERNEL);
9316                 if (qpair == NULL) {
9317                         ql_log(ql_log_warn, vha, 0x0182,
9318                             "Failed to allocate memory for queue pair.\n");
9319                         return NULL;
9320                 }
9321
9322                 qpair->hw = vha->hw;
9323                 qpair->vha = vha;
9324                 qpair->qp_lock_ptr = &qpair->qp_lock;
9325                 spin_lock_init(&qpair->qp_lock);
9326                 qpair->use_shadow_reg = IS_SHADOW_REG_CAPABLE(ha) ? 1 : 0;
9327
9328                 /* Assign available que pair id */
9329                 mutex_lock(&ha->mq_lock);
9330                 qpair_id = find_first_zero_bit(ha->qpair_qid_map, ha->max_qpairs);
9331                 if (ha->num_qpairs >= ha->max_qpairs) {
9332                         mutex_unlock(&ha->mq_lock);
9333                         ql_log(ql_log_warn, vha, 0x0183,
9334                             "No resources to create additional q pair.\n");
9335                         goto fail_qid_map;
9336                 }
9337                 ha->num_qpairs++;
9338                 set_bit(qpair_id, ha->qpair_qid_map);
9339                 ha->queue_pair_map[qpair_id] = qpair;
9340                 qpair->id = qpair_id;
9341                 qpair->vp_idx = vp_idx;
9342                 qpair->fw_started = ha->flags.fw_started;
9343                 INIT_LIST_HEAD(&qpair->hints_list);
9344                 qpair->chip_reset = ha->base_qpair->chip_reset;
9345                 qpair->enable_class_2 = ha->base_qpair->enable_class_2;
9346                 qpair->enable_explicit_conf =
9347                     ha->base_qpair->enable_explicit_conf;
9348
9349                 for (i = 0; i < ha->msix_count; i++) {
9350                         msix = &ha->msix_entries[i];
9351                         if (msix->in_use)
9352                                 continue;
9353                         qpair->msix = msix;
9354                         ql_dbg(ql_dbg_multiq, vha, 0xc00f,
9355                             "Vector %x selected for qpair\n", msix->vector);
9356                         break;
9357                 }
9358                 if (!qpair->msix) {
9359                         ql_log(ql_log_warn, vha, 0x0184,
9360                             "Out of MSI-X vectors!.\n");
9361                         goto fail_msix;
9362                 }
9363
9364                 qpair->msix->in_use = 1;
9365                 list_add_tail(&qpair->qp_list_elem, &vha->qp_list);
9366                 qpair->pdev = ha->pdev;
9367                 if (IS_QLA27XX(ha) || IS_QLA83XX(ha) || IS_QLA28XX(ha))
9368                         qpair->reqq_start_iocbs = qla_83xx_start_iocbs;
9369
9370                 mutex_unlock(&ha->mq_lock);
9371
9372                 /* Create response queue first */
9373                 rsp_id = qla25xx_create_rsp_que(ha, 0, 0, 0, qpair, startqp);
9374                 if (!rsp_id) {
9375                         ql_log(ql_log_warn, vha, 0x0185,
9376                             "Failed to create response queue.\n");
9377                         goto fail_rsp;
9378                 }
9379
9380                 qpair->rsp = ha->rsp_q_map[rsp_id];
9381
9382                 /* Create request queue */
9383                 req_id = qla25xx_create_req_que(ha, 0, vp_idx, 0, rsp_id, qos,
9384                     startqp);
9385                 if (!req_id) {
9386                         ql_log(ql_log_warn, vha, 0x0186,
9387                             "Failed to create request queue.\n");
9388                         goto fail_req;
9389                 }
9390
9391                 qpair->req = ha->req_q_map[req_id];
9392                 qpair->rsp->req = qpair->req;
9393                 qpair->rsp->qpair = qpair;
9394                 /* init qpair to this cpu. Will adjust at run time. */
9395                 qla_cpu_update(qpair, smp_processor_id());
9396
9397                 if (IS_T10_PI_CAPABLE(ha) && ql2xenabledif) {
9398                         if (ha->fw_attributes & BIT_4)
9399                                 qpair->difdix_supported = 1;
9400                 }
9401
9402                 qpair->srb_mempool = mempool_create_slab_pool(SRB_MIN_REQ, srb_cachep);
9403                 if (!qpair->srb_mempool) {
9404                         ql_log(ql_log_warn, vha, 0xd036,
9405                             "Failed to create srb mempool for qpair %d\n",
9406                             qpair->id);
9407                         goto fail_mempool;
9408                 }
9409
9410                 /* Mark as online */
9411                 qpair->online = 1;
9412
9413                 if (!vha->flags.qpairs_available)
9414                         vha->flags.qpairs_available = 1;
9415
9416                 ql_dbg(ql_dbg_multiq, vha, 0xc00d,
9417                     "Request/Response queue pair created, id %d\n",
9418                     qpair->id);
9419                 ql_dbg(ql_dbg_init, vha, 0x0187,
9420                     "Request/Response queue pair created, id %d\n",
9421                     qpair->id);
9422         }
9423         return qpair;
9424
9425 fail_mempool:
9426 fail_req:
9427         qla25xx_delete_rsp_que(vha, qpair->rsp);
9428 fail_rsp:
9429         mutex_lock(&ha->mq_lock);
9430         qpair->msix->in_use = 0;
9431         list_del(&qpair->qp_list_elem);
9432         if (list_empty(&vha->qp_list))
9433                 vha->flags.qpairs_available = 0;
9434 fail_msix:
9435         ha->queue_pair_map[qpair_id] = NULL;
9436         clear_bit(qpair_id, ha->qpair_qid_map);
9437         ha->num_qpairs--;
9438         mutex_unlock(&ha->mq_lock);
9439 fail_qid_map:
9440         kfree(qpair);
9441         return NULL;
9442 }
9443
9444 int qla2xxx_delete_qpair(struct scsi_qla_host *vha, struct qla_qpair *qpair)
9445 {
9446         int ret = QLA_FUNCTION_FAILED;
9447         struct qla_hw_data *ha = qpair->hw;
9448
9449         qpair->delete_in_progress = 1;
9450
9451         ret = qla25xx_delete_req_que(vha, qpair->req);
9452         if (ret != QLA_SUCCESS)
9453                 goto fail;
9454
9455         ret = qla25xx_delete_rsp_que(vha, qpair->rsp);
9456         if (ret != QLA_SUCCESS)
9457                 goto fail;
9458
9459         mutex_lock(&ha->mq_lock);
9460         ha->queue_pair_map[qpair->id] = NULL;
9461         clear_bit(qpair->id, ha->qpair_qid_map);
9462         ha->num_qpairs--;
9463         list_del(&qpair->qp_list_elem);
9464         if (list_empty(&vha->qp_list)) {
9465                 vha->flags.qpairs_available = 0;
9466                 vha->flags.qpairs_req_created = 0;
9467                 vha->flags.qpairs_rsp_created = 0;
9468         }
9469         mempool_destroy(qpair->srb_mempool);
9470         kfree(qpair);
9471         mutex_unlock(&ha->mq_lock);
9472
9473         return QLA_SUCCESS;
9474 fail:
9475         return ret;
9476 }
9477
9478 uint64_t
9479 qla2x00_count_set_bits(uint32_t num)
9480 {
9481         /* Brian Kernighan's Algorithm */
9482         u64 count = 0;
9483
9484         while (num) {
9485                 num &= (num - 1);
9486                 count++;
9487         }
9488         return count;
9489 }
9490
9491 uint64_t
9492 qla2x00_get_num_tgts(scsi_qla_host_t *vha)
9493 {
9494         fc_port_t *f, *tf;
9495         u64 count = 0;
9496
9497         f = NULL;
9498         tf = NULL;
9499
9500         list_for_each_entry_safe(f, tf, &vha->vp_fcports, list) {
9501                 if (f->port_type != FCT_TARGET)
9502                         continue;
9503                 count++;
9504         }
9505         return count;
9506 }
9507
9508 int qla2xxx_reset_stats(struct Scsi_Host *host, u32 flags)
9509 {
9510         scsi_qla_host_t *vha = shost_priv(host);
9511         fc_port_t *fcport = NULL;
9512         unsigned long int_flags;
9513
9514         if (flags & QLA2XX_HW_ERROR)
9515                 vha->hw_err_cnt = 0;
9516         if (flags & QLA2XX_SHT_LNK_DWN)
9517                 vha->short_link_down_cnt = 0;
9518         if (flags & QLA2XX_INT_ERR)
9519                 vha->interface_err_cnt = 0;
9520         if (flags & QLA2XX_CMD_TIMEOUT)
9521                 vha->cmd_timeout_cnt = 0;
9522         if (flags & QLA2XX_RESET_CMD_ERR)
9523                 vha->reset_cmd_err_cnt = 0;
9524         if (flags & QLA2XX_TGT_SHT_LNK_DOWN) {
9525                 spin_lock_irqsave(&vha->hw->tgt.sess_lock, int_flags);
9526                 list_for_each_entry(fcport, &vha->vp_fcports, list) {
9527                         fcport->tgt_short_link_down_cnt = 0;
9528                         fcport->tgt_link_down_time = QLA2XX_MAX_LINK_DOWN_TIME;
9529                 }
9530                 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, int_flags);
9531         }
9532         vha->link_down_time = QLA2XX_MAX_LINK_DOWN_TIME;
9533         return 0;
9534 }
9535
9536 int qla2xxx_start_stats(struct Scsi_Host *host, u32 flags)
9537 {
9538         return qla2xxx_reset_stats(host, flags);
9539 }
9540
9541 int qla2xxx_stop_stats(struct Scsi_Host *host, u32 flags)
9542 {
9543         return qla2xxx_reset_stats(host, flags);
9544 }
9545
9546 int qla2xxx_get_ini_stats(struct Scsi_Host *host, u32 flags,
9547                           void *data, u64 size)
9548 {
9549         scsi_qla_host_t *vha = shost_priv(host);
9550         struct ql_vnd_host_stats_resp *resp = (struct ql_vnd_host_stats_resp *)data;
9551         struct ql_vnd_stats *rsp_data = &resp->stats;
9552         u64 ini_entry_count = 0;
9553         u64 i = 0;
9554         u64 entry_count = 0;
9555         u64 num_tgt = 0;
9556         u32 tmp_stat_type = 0;
9557         fc_port_t *fcport = NULL;
9558         unsigned long int_flags;
9559
9560         /* Copy stat type to work on it */
9561         tmp_stat_type = flags;
9562
9563         if (tmp_stat_type & BIT_17) {
9564                 num_tgt = qla2x00_get_num_tgts(vha);
9565                 /* unset BIT_17 */
9566                 tmp_stat_type &= ~(1 << 17);
9567         }
9568         ini_entry_count = qla2x00_count_set_bits(tmp_stat_type);
9569
9570         entry_count = ini_entry_count + num_tgt;
9571
9572         rsp_data->entry_count = entry_count;
9573
9574         i = 0;
9575         if (flags & QLA2XX_HW_ERROR) {
9576                 rsp_data->entry[i].stat_type = QLA2XX_HW_ERROR;
9577                 rsp_data->entry[i].tgt_num = 0x0;
9578                 rsp_data->entry[i].cnt = vha->hw_err_cnt;
9579                 i++;
9580         }
9581
9582         if (flags & QLA2XX_SHT_LNK_DWN) {
9583                 rsp_data->entry[i].stat_type = QLA2XX_SHT_LNK_DWN;
9584                 rsp_data->entry[i].tgt_num = 0x0;
9585                 rsp_data->entry[i].cnt = vha->short_link_down_cnt;
9586                 i++;
9587         }
9588
9589         if (flags & QLA2XX_INT_ERR) {
9590                 rsp_data->entry[i].stat_type = QLA2XX_INT_ERR;
9591                 rsp_data->entry[i].tgt_num = 0x0;
9592                 rsp_data->entry[i].cnt = vha->interface_err_cnt;
9593                 i++;
9594         }
9595
9596         if (flags & QLA2XX_CMD_TIMEOUT) {
9597                 rsp_data->entry[i].stat_type = QLA2XX_CMD_TIMEOUT;
9598                 rsp_data->entry[i].tgt_num = 0x0;
9599                 rsp_data->entry[i].cnt = vha->cmd_timeout_cnt;
9600                 i++;
9601         }
9602
9603         if (flags & QLA2XX_RESET_CMD_ERR) {
9604                 rsp_data->entry[i].stat_type = QLA2XX_RESET_CMD_ERR;
9605                 rsp_data->entry[i].tgt_num = 0x0;
9606                 rsp_data->entry[i].cnt = vha->reset_cmd_err_cnt;
9607                 i++;
9608         }
9609
9610         /* i will continue from previous loop, as target
9611          * entries are after initiator
9612          */
9613         if (flags & QLA2XX_TGT_SHT_LNK_DOWN) {
9614                 spin_lock_irqsave(&vha->hw->tgt.sess_lock, int_flags);
9615                 list_for_each_entry(fcport, &vha->vp_fcports, list) {
9616                         if (fcport->port_type != FCT_TARGET)
9617                                 continue;
9618                         if (!fcport->rport)
9619                                 continue;
9620                         rsp_data->entry[i].stat_type = QLA2XX_TGT_SHT_LNK_DOWN;
9621                         rsp_data->entry[i].tgt_num = fcport->rport->number;
9622                         rsp_data->entry[i].cnt = fcport->tgt_short_link_down_cnt;
9623                         i++;
9624                 }
9625                 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, int_flags);
9626         }
9627         resp->status = EXT_STATUS_OK;
9628
9629         return 0;
9630 }
9631
9632 int qla2xxx_get_tgt_stats(struct Scsi_Host *host, u32 flags,
9633                           struct fc_rport *rport, void *data, u64 size)
9634 {
9635         struct ql_vnd_tgt_stats_resp *tgt_data = data;
9636         fc_port_t *fcport = *(fc_port_t **)rport->dd_data;
9637
9638         tgt_data->status = 0;
9639         tgt_data->stats.entry_count = 1;
9640         tgt_data->stats.entry[0].stat_type = flags;
9641         tgt_data->stats.entry[0].tgt_num = rport->number;
9642         tgt_data->stats.entry[0].cnt = fcport->tgt_short_link_down_cnt;
9643
9644         return 0;
9645 }
9646
9647 int qla2xxx_disable_port(struct Scsi_Host *host)
9648 {
9649         scsi_qla_host_t *vha = shost_priv(host);
9650
9651         vha->hw->flags.port_isolated = 1;
9652
9653         if (qla2x00_chip_is_down(vha))
9654                 return 0;
9655
9656         if (vha->flags.online) {
9657                 qla2x00_abort_isp_cleanup(vha);
9658                 qla2x00_wait_for_sess_deletion(vha);
9659         }
9660
9661         return 0;
9662 }
9663
9664 int qla2xxx_enable_port(struct Scsi_Host *host)
9665 {
9666         scsi_qla_host_t *vha = shost_priv(host);
9667
9668         vha->hw->flags.port_isolated = 0;
9669         /* Set the flag to 1, so that isp_abort can proceed */
9670         vha->flags.online = 1;
9671         set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
9672         qla2xxx_wake_dpc(vha);
9673
9674         return 0;
9675 }