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