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