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