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