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