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